The fastest way to check if the WCF service is running

I have a WCF service running locally. The service has a default port on which it is running, but if this port is already in use, I assign the port dynamically.

I added this to the host to make it available:

serviceHost.Description.Behaviors.Add(new ServiceDiscoveryBehavior()); serviceHost.AddServiceEndpoint(new UdpDiscoveryEndpoint()); 

Now my client needs to connect to this host. In the client, I want him to try the default port first, and if he cannot connect to the service on the default port, then he will open.

I found that this discovery takes about 20-30 seconds, so I would prefer to avoid it only when it cannot find the host on the default port.

So my question is: what is the fastest way to determine if my host is on the default port?

I was thinking of doing something like setting an open timeout on the client for 10 seconds and then trying / catching on open, but it still takes 10 seconds to wait.

+4
source share
2 answers

So calling client.Open(); will not throw any exceptions if your endpoint is incorrect.

I added a method in my service called IsAlive() , which simply returns true. Therefore, I call client.IsAlive(); after opening, and if the endpoint is incorrect, there will be an exception almost instantly.

+5
source

In fact, you can just go to the URL in the browser, which should show you if the web service is working. Therefore, if the WCF URL

 http://YourService:8000/YourClass/YourMethod 

entering text into the address bar of the browser should show you the WSDL page if the service is running, error 404 if not.

My WCF service is self-organized, so I will also add that when my WCF service starts, I use this line of code to output to DOS windows what is the current URL of my service:

 Console.WriteLine("HostInterface running on http://" & Domain & ":" & Port & "/Service/Endpoint" ) 
+4
source

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


All Articles