Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Quickest way to check if WCF service is running

Tags:

c#

port

tcp

wcf

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

I added this in the host to make it discoverable:

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

Now my client needs to connect to this host. In the client I want to have it first try the default port, and if it can't connect to the service on the default port, then it will do the discovery.

I've found that the discovery takes about 20-30 seconds, so I would prefer to avoid always doing it, only when it can't find the host on the default port.

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

I was thinking about doing something like setting the open timeout on the client to 10 seconds, and then doing a try/catch on open, but that still requires waiting 10 seconds.

like image 609
NMunro Avatar asked Aug 02 '13 15:08

NMunro


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. So I call client.IsAlive(); after opening and if the endpoint is incorrect there will be an exception almost instantly.

like image 101
NMunro Avatar answered Oct 17 '22 23:10

NMunro


You can actually just navigate to the URL in a browser, which should show you if the web service is running. So if the WCF url is

http://YourService:8000/YourClass/YourMethod

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

My WCF service is self hosted, so I'll add, too, that when my WCF service starts, I use a this line of code to output into a DOS windows what the current URL of my service is:

Console.WriteLine("HostInterface running on http://" & Domain & ":" & Port & "/Service/Endpoint" )
like image 26
Brian Avatar answered Oct 18 '22 00:10

Brian