20

I need to create an C# application that will monitor whether a set of web services are up and running. User will select a service name from a dropdown. The program need to test with the corresponding service URL and show whether the service is running. What is the best way to do it? One way I am thinking of is to test whether we are able to download the wsdl. IS there a better way?

Note: The purpose of this application is that the user need to know only the service name. He need not remember/store the corresponding URL of the service.

I need a website version and a desktop application version of this C# application.

Note: Existing services are using WCF. But in future a non-WCF service may get added.

Note: My program will not be aware of (or not interested in ) operations in the service. So I cannot call a service operation.

REFERENCE

  1. How to check if a web service is up and running without using ping?
  2. C program-How do I check if a web service is running
Community
  • 1
  • 1
LCJ
  • 22,196
  • 67
  • 260
  • 418
  • 1
    These services that will be monitored are pre-configured, right? I mean, is this meant to be a generic webservice monitoring tool? – Andre Calil Aug 23 '12 at 14:34
  • 3
    Downloading the WSDL would verify that the _site_ is running but wouldn't tell you if a dependency of the web service (database, reference, etc.) is down or missing. – D Stanley Aug 23 '12 at 14:35
  • 5
    I typically add a `Version` method to my service that just returns a "version string" from that service - if it's up and running. But really: that doesn't tell you anything.... your call work **now** - but there's no guarantee that a nanosecond later, it'll still work..... you just need to program defensively and be prepared at all times that a service call can fail or timeout or anything else.... – marc_s Aug 23 '12 at 14:35
  • 2
    What is the definition of "Up and running"? A HTTP 200 status code code very well mean that the backend database has still crashed. – Uwe Keim Aug 23 '12 at 14:36
  • Indeed. The **best** approach would be to have a *heartbeat* method at the service, but you would depend on the service implementation. AFAIK, creating a new reference of a SOAP webservice object will cause the WSDL to be downloaded and checked agains the class signature. If you are able to instantiate the object without any excpetions, then the service is (kind of) OK. – Andre Calil Aug 23 '12 at 14:37
  • 1
    I believe you can disable the downloading of WSDL so that may not be the best way. Someone please correct me if I'm wrong. – mj_ Aug 23 '12 at 14:37
  • @UweKeim What C# code can be used to check whether it returns status code 200? That will probably the answer for this question. – LCJ Aug 23 '12 at 14:37
  • 1
    Write a console application and Call webservice with some fixed parameters and check returned result and compare with what you expect ? Is this something you can do ? – Pit Digger Aug 23 '12 at 14:38
  • @Lijo Personally, I would define one single `WebMethod` to call that does internal checking of all relevant systems. – Uwe Keim Aug 23 '12 at 14:38
  • @UweKeim My program will not be aware of (or not interested in ) operations in the service. So I cannot call a service operation. – LCJ Aug 23 '12 at 14:41
  • Won't this be the same as checking a web site is accesible? If you won't query the interface and assume nothing about the interface then all you can do is check that the url is accesible. – Jodrell Aug 23 '12 at 14:50

2 Answers2

41

this would not guarantee functionality, but at least you could check connectivity to a URL:

var url = "http://url.to.che.ck/serviceEndpoint.svc";

try
{
    var myRequest = (HttpWebRequest)WebRequest.Create(url);

    var response = (HttpWebResponse)myRequest.GetResponse();

    if (response.StatusCode == HttpStatusCode.OK)
    {
        //  it's at least in some way responsive
        //  but may be internally broken
        //  as you could find out if you called one of the methods for real
        Debug.Write(string.Format("{0} Available", url));
    }
    else
    {
        //  well, at least it returned...
        Debug.Write(string.Format("{0} Returned, but with status: {1}", 
            url, response.StatusDescription));
    }
}
catch (Exception ex)
{
    //  not available at all, for some reason
    Debug.Write(string.Format("{0} unavailable: {1}", url, ex.Message));
}
Daniel Dušek
  • 13,683
  • 5
  • 36
  • 51
paul
  • 21,653
  • 1
  • 53
  • 54
  • 2
    is there any other way to check if the process is up and running? i'm trying your suggestion `GetResponse()` method but its taking much more time.. – RobertKing Jan 15 '14 at 07:15
  • 2
    And here's how I get my service uri: `var url = ServiceClient().Endpoint.Address.Uri;` – Savage Aug 04 '16 at 15:39
  • If you need it, you can set myRequest.UseDefaultCredentials = true – peter70 Aug 23 '22 at 10:09
7

This approach works for me.

I used Socket to check if the process can connect. HttpWebRequest works if you try to check the connection 1-3 times but if you have a process which will run 24hours and from time to time needs to check the webserver availability that will not work anymore because will throw TimeOut Exception.

Socket socket 
   = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);

var result = socket.BeginConnect("xxx.com", 80, null, null);

// test the connection for 3 seconds
bool success = result.AsyncWaitHandle.WaitOne(3000,false); 

var resturnVal = socket.Connected;
if (socket.Connected)
    socket.Disconnect(true);
                
socket.Dispose();
                
return resturnVal;
ΩmegaMan
  • 29,542
  • 12
  • 100
  • 122
Victor
  • 71
  • 1
  • 1
  • 4
    I used this answer for my solution, worked well and succinctly. However, since Socket is disposable, I wrapped the entire code block in a `using` block. i.e.: `using(Socket socket = new Socket(...)){ ... }` – Rook Jan 31 '18 at 23:30