static void Main(string[] args) { try { Person person = new Person("John Doe"); IChatServiceCallback callback = new SimpleChatCallback(); InstanceContext instanceContext = new InstanceContext(callback); ChatServiceClient serviceProxy = new ChatServiceClient(instanceContext); Console.WriteLine("Endpoint:"); Console.WriteLine("***********************************************"); Console.WriteLine(string.Format("Address = {0}", serviceProxy.Endpoint.Address)); Console.WriteLine(string.Format("Binding = {0}", serviceProxy.Endpoint.Binding)); Console.WriteLine(string.Format("Contract = {0}", serviceProxy.Endpoint.Contract.Name)); Person[] people = serviceProxy.Join(person); Console.WriteLine("***********************************************"); Console.WriteLine("Connected !"); Console.WriteLine("Online users:"); foreach (Person p in people) { Console.WriteLine(p.Name); } Console.WriteLine("Press <ENTER> to finish..."); Console.ReadLine(); if (serviceProxy.State != CommunicationState.Faulted) { serviceProxy.Close(); } } catch (Exception ex) { Console.WriteLine(ex.Message); } }
public void OnDisconnect(DateTime dateTime, bool result, string message) { try { client.Close(); } catch { // Ignore any error client.Abort(); } finally { client = null; Disconnected(this, result, dateTime, message); } }
public void MainWindow_Closing(object sender, CancelEventArgs e) { if (MessageBox.Show("Are you sure you want to close the application?", "Chat Application", MessageBoxButton.YesNo) == MessageBoxResult.No) { e.Cancel = true; } else { if (!string.IsNullOrEmpty(Username)) { server.LogOut(); } server.Close(); } }
private void Disconnect() { try { _chatClient.Disconnect(); _chatClient.Close(); } catch (Exception e) { _dispatcher.Invoke(new Action(() => AppendText(String.Format("Couldn't establish connection to server. {0} {1}", e.Message, e.InnerException != null ? e.InnerException.Message : ""), Colors.Red))); _dispatcher.Invoke(new Action(() => EnableChat(false))); _dispatcher.Invoke(new Action(() => EnableConnect(true))); _dispatcher.Invoke(new Action(() => EnableDisconnect(false))); } }
private T Call <T>(Func <ChatServiceClient, T> f) { var chatClient = new ChatServiceClient(); try { var result = f(chatClient); chatClient.Close(); return(result); } catch (Exception) { chatClient.Abort(); throw; } }
static void Main(string[] args) { try { if (args.Length != 1) { Console.WriteLine("usage: clientconsole username"); } else { Person user = new Person(args[0]); IChatServiceCallback callback = new SimpleChatCallback(); InstanceContext instanceContext = new InstanceContext(callback); ChatServiceClient serviceProxy = new ChatServiceClient(instanceContext); Console.WriteLine("Endpoint:"); Console.WriteLine("***********************************************"); Console.WriteLine(string.Format("Address = {0}", serviceProxy.Endpoint.Address)); Console.WriteLine(string.Format("Binding = {0}", serviceProxy.Endpoint.Binding)); Console.WriteLine(string.Format("Contract = {0}", serviceProxy.Endpoint.Contract.Name)); Person[] people = serviceProxy.Join(user); Console.WriteLine("***********************************************"); Console.WriteLine("Connected !"); Console.WriteLine("Online users:"); foreach (Person p in people) { Console.WriteLine(p.Name); } string msg; while ((msg = Console.ReadLine()) != "exit") { serviceProxy.Say(msg); } serviceProxy.Leave(); if (serviceProxy.State != CommunicationState.Faulted) { serviceProxy.Close(); } } } catch (Exception ex) { Console.WriteLine(ex.Message); } }
private void LogOutButton_Click(object sender, RoutedEventArgs e) { //Step 3: Closing the client gracefully closes the connection and cleans up resources. proxy.Close(); Close(); }
private void This_Closed(object sender, EventArgs e) { WriteConfig(_configPath); _chatClient.Close(); }