sekar
sekar

Reputation:

check the availability of the WCF Web Service

I want to check the availability of the WCF web service i.c service is up or down through the C# code. How to achieve that?

Upvotes: 10

Views: 8311

Answers (4)

flobadob
flobadob

Reputation: 2872

I use the following code. It's simple and works...

    public bool IsServiceRunning()
    {
        try
        {
            System.Net.WebClient wc = new System.Net.WebClient();
            string s = wc.DownloadString(new Uri("http://localhost:27777/whatever/services/GatherDataService?wsdl"));
        }
        catch (Exception ex)
        {
            return false;
        }

        return true;
    }

just take your endpoint uri and add the ?wsdl

Upvotes: 1

Dave_B
Dave_B

Reputation: 29

This is what I'm using and it works great. And ServiceController lives in namespace 'System.ServiceProcess' if you want to use a Using statement at the top to qualify it.

try
{
    ServiceController sc = new ServiceController("Service Name", "Computer's IP Address");
    Console.WriteLine("The service status is currently set to {0}",
        sc.Status.ToString());

    if ((sc.Status.Equals(ServiceControllerStatus.Stopped)) ||
        (sc.Status.Equals(ServiceControllerStatus.StopPending)))
    {
        Console.WriteLine("Service is Stopped, Ending the application...");
        Console.Read();
        EndApplication();
    }
    else
    {
        Console.WriteLine("Service is Started...");
    }
}
catch (Exception)
{
    Console.WriteLine("Error Occurred trying to access the Server service...");
    Console.Read();
    EndApplication();
}

Upvotes: 1

John Saunders
John Saunders

Reputation: 161801

To elaborate on the previous answer: the only way to determine if a service is "available" is to first determine what you mean by "available". For instance, a service that depends on an external resource like a database may be perfectly available, but if the database cannot be accessed, then the service will be available but useless.

You should also ask what you are going to do with the information about availability. In particular, what would happen if you decided that the service was "available" yet, when you call it, you find that it is not really "available". An example would be if the above service was available and the database was available, but there was one particular stored procedure which would always fail. Is the service "available" in this case? How bad would it be if you indicated that it was available, but this one stored procedure failed?

In many cases, it's best to simply go ahead and make the calls to the web service, then handle any exceptions. If you've validated the parameters you're sending to the service, then, from the point of view of the end user, any failure of the service amounts to the service being unavailable.

It is not available to be successfully used, you see.

Upvotes: 7

Robert MacLean
Robert MacLean

Reputation: 39260

When you call Client.Open if it is down that should throw an exception which you can trap.
What I prefer to do is implement a method which returns a boolean called Ping. The code basically just does return true; so it returns as quickly as possible. On the client side I call it and trap exceptions, if I get any then I know the web service is down.
You can extend the pattern to do things like PingCheckDB or PingCheckX which can do a fake/sample test run so you enable/disable functionality on the client based on what is available.

Upvotes: 9

Related Questions