If you use ChannelFactory.CreateChannel , it returns a proxy server that implements the interface of your service without opening a communication channel. However, the proxy server also implements IClientChannel , which can be used to verify the IClientChannel endpoint. In my case, I also needed to wait for the endpoint to start, so I used a timeout loop:
public static Interface CreateOpenChannel<Interface>(Binding protocol, EndpointAddress address, int timeoutMs = 5000) { for (int startTime = Environment.TickCount;;) { // a proxy is unusable after comm failure ("faulted" state), so create it within the loop Interface proxy = ChannelFactory<Interface>.CreateChannel(protocol, address); try { ((IClientChannel) proxy).Open(); return proxy; } catch (CommunicationException ex) { if (unchecked(Environment.TickCount - startTime) >= timeoutMs) throw; Thread.Sleep(Math.Min(1000, timeoutMs / 4)); } } }
This has been verified with NetNamedPipeBinding . I'm not sure if this will behave the same way with other bindings, or Open() just opens the connection or also checks the validity of the connection (for example, does the host interface match the client interface).
source share