How can I check if the network is listening?

How can I check programmatically to see if any particular network is working and listening, so I do not get the exception "There was no listening to the endpoint ..."?

So for example, if I have this code:

Uri baseAddress = new Uri("http://localhost/something"); var _ServiceHost = new ServiceHost(typeof(Automation), new Uri[] { baseAddress }); NetNamedPipeBinding nnpb = new NetNamedPipeBinding(); _ServiceHost.AddServiceEndpoint(typeof(IAutomation), nnpb, "ImListening"); _ServiceHost.Open(); 

I want from another application to communicate with http://localhost/something/ImListening , but before I want to make sure that this is listening, I do not get an exception or is this the only way to check this?

+5
source share
4 answers

Listen to the exception. This is the right way to do this.

+1
source

Exceptions exist for some reason, I would just handle the exception, since you are handling it, the user will not receive a cryptic error message, which I think is what you are trying to avoid.

+1
source

This may not be the best way, but I have not found another way to test the endpoint using NetNamedPipe .

Usually I do this approach:

  public void IsValid() { RegisterConfiguration(); var endPoints = Host.Description.Endpoints; if (!endPoints.HasElements()) { throw new NullReferenceException("endpoints (WCF Service)."); } foreach (var item in endPoints) { var service = new ChannelFactory<ITcoService>(item.Binding, item.Address); try { var client = (IClientChannel)service.CreateChannel(); try { client.Open(TimeSpan.FromSeconds(2)); throw new InvalidOperationException( string.Format( "A registration already exists for URI: \"{0}\" (WCF Service is already open in some IChannelListener).", item.Address)); } catch (Exception ex) { if (ex is System.ServiceModel.CommunicationObjectFaultedException || ex is System.ServiceModel.EndpointNotFoundException) { Debug.WriteLine(ex.DumpObject()); } else { throw; } } finally { new Action(client.Dispose).InvokeSafe(); } } finally { new Action(service.Close).InvokeSafe(); } } } 

(sorry that the extension methods in this code, InvokeSafe is just a try / catch to execute the Action and HasElements tags only if the collection is different from empty and empty).

0
source

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).

0
source

Source: https://habr.com/ru/post/1387145/


All Articles