private static void HandleError() { _messageListener = null; _serverObject = null; _clientObject = null; if (_channel != null) { ChannelServices.UnregisterChannel(_channel); _channel = null; } IsConnected = false; }
public static void Connect(IMessageListener messageListener) { Timer.Change(0, Timeout.Infinite); if (messageListener == null) return; if (string.IsNullOrWhiteSpace(LocalSettings.MessagingServerName)) return; _messageListener = messageListener; var serverProv = new BinaryServerFormatterSinkProvider { TypeFilterLevel = System.Runtime.Serialization.Formatters.TypeFilterLevel.Full }; var clientProv = new BinaryClientFormatterSinkProvider(); IDictionary props = new Hashtable(); props["port"] = 0; _channel = new TcpChannel(props, clientProv, serverProv); ChannelServices.RegisterChannel(_channel, false); var url = String.Format("tcp://{0}:{1}/ChatServer", LocalSettings.MessagingServerName, LocalSettings.MessagingServerPort); try { _serverObject = (MessagingServerObject)Activator.GetObject(typeof(MessagingServerObject), url); _clientObject = new MessagingClientObject(); _serverObject.Attach(_clientObject); } catch { HandleError(); return; } IsConnected = true; Timer.Change(0, 1000); }