C#: How to programmatically check a web service is up and running?

70,476

Solution 1

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));
}

Solution 2

I may be too late with the answer but 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);
                bool success = result.AsyncWaitHandle.WaitOne(3000,false); // test the connection for 3 seconds
                var resturnVal = socket.Connected;
                if (socket.Connected)
                    socket.Disconnect(true);
                socket.Dispose();
                return resturnVal;
Share:
70,476

Related videos on Youtube

LCJ
Author by

LCJ

.Net / C#/ SQL Server Developer Some of my posts listed below -- http://stackoverflow.com/questions/3618380/log4net-does-not-write-the-log-file/14682889#14682889 http://stackoverflow.com/questions/11549943/datetime-field-overflow-with-ibm-data-server-client-v9-7fp5/14215249#14215249 http://stackoverflow.com/questions/12420314/one-wcf-service-two-clients-one-client-does-not-work/12425653#12425653 http://stackoverflow.com/questions/18014392/select-sql-server-database-size/25452709#25452709 http://stackoverflow.com/questions/22589245/difference-between-mvc-5-project-and-web-api-project/25036611#25036611 http://stackoverflow.com/questions/4511346/wsdl-whats-the-difference-between-binding-and-porttype/15408410#15408410 http://stackoverflow.com/questions/7530725/unrecognized-attribute-targetframework-note-that-attribute-names-are-case-sen/18351068#18351068 http://stackoverflow.com/questions/9470013/do-not-use-abstract-base-class-in-design-but-in-modeling-analysis http://stackoverflow.com/questions/11578374/entity-framework-4-0-how-to-see-sql-statements-for-savechanges-method http://stackoverflow.com/questions/14486733/how-to-check-whether-postback-caused-by-a-dynamic-link-button

Updated on July 09, 2022

Comments

  • LCJ
    LCJ almost 2 years

    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
    • Andre Calil
      Andre Calil over 11 years
      These services that will be monitored are pre-configured, right? I mean, is this meant to be a generic webservice monitoring tool?
    • D Stanley
      D Stanley over 11 years
      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.
    • marc_s
      marc_s over 11 years
      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....
    • Uwe Keim
      Uwe Keim over 11 years
      What is the definition of "Up and running"? A HTTP 200 status code code very well mean that the backend database has still crashed.
    • Andre Calil
      Andre Calil over 11 years
      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.
    • mj_
      mj_ over 11 years
      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.
    • LCJ
      LCJ over 11 years
      @UweKeim What C# code can be used to check whether it returns status code 200? That will probably the answer for this question.
    • Pit Digger
      Pit Digger over 11 years
      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 ?
    • Uwe Keim
      Uwe Keim over 11 years
      @Lijo Personally, I would define one single WebMethod to call that does internal checking of all relevant systems.
    • LCJ
      LCJ over 11 years
      @UweKeim My program will not be aware of (or not interested in ) operations in the service. So I cannot call a service operation.
    • Jodrell
      Jodrell over 11 years
      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.
  • RobertKing
    RobertKing over 10 years
    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..
  • Savage
    Savage over 7 years
    And here's how I get my service uri: var url = ServiceClient().Endpoint.Address.Uri;
  • Rook
    Rook about 6 years
    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(...)){ ... }