public void Connect() { //Discover WCF service via broadcasting _broadcastSearch = new Thread(() => { DiscoveryClient discoveryClient = new DiscoveryClient(new UdpDiscoveryEndpoint()); Collection <EndpointDiscoveryMetadata> helloWorldServices = discoveryClient.Find(new FindCriteria(typeof(IControllerService))).Endpoints; discoveryClient.Close(); if (helloWorldServices.Count == 0) { OnConnectionFailed(); Trace.TraceWarning("Client not found"); } else { Trace.TraceInformation("Connecting to client"); EndpointAddress serviceAddress = helloWorldServices[0].Address; NetTcpBinding binding = new NetTcpBinding(SecurityMode.None); binding.ReliableSession.Enabled = true; binding.ReliableSession.Ordered = false; IControllerServiceCallback evnt = new ControllerCallback(this); InstanceContext evntCntx = new InstanceContext(evnt); proxy = new EventServiceController(evntCntx, binding, serviceAddress); proxy.ChannelFactory.Faulted += new EventHandler(ChannelFactory_Faulted); proxy.Connect(); } }); _broadcastSearch.Start(); }
public void Close() { try { if (_broadcastSearch.IsAlive) { _broadcastSearch.Abort(); } if (proxy == null) { return; } if (proxy.State == CommunicationState.Faulted) { proxy.Abort(); } else { proxy.DisconnectPrepare(); proxy.Close(); } proxy = null; } catch (Exception e) { Trace.TraceError("Something happend at closing connection: {0}", e); } }