views:

1945

answers:

4

I'm busy writing a class that monitors the status of RAS connections. I need to test to make sure that the connection is not only connected, but also that it can communicate with my web service. Since this class will be used in many future projects, I'd like a way to test the connection to the webservice without knowing anything about it.

I was thinking of passing the URL to the class so that it at least knows where to find it. Pinging the server is not a sufficient test. It is possible for the server to be available, but the service to be offline.

How can I effectively test that I'm able to get a response from the web service?

+4  A: 

You are right that pinging the server isn't sufficient. The server can be up, but the web service unavailable due to a number of reasons.

To monitor our web service connections, I created a IMonitoredService interface that has a method CheckService(). The wrapper class for each web service implements this method to call an innocuous method on the web service and reports if the service is up or not. This allows any number of services to be monitored with out the code responsible for the monitoring knowing the details of the service.

If you know a bit about what the web service will return from accessing the URL directly, you could try just using the URL. For example, Microsoft's asmx file returns a summary of the web service. Other implementations may behave differently though.

g .
I think that the interface might just be the way to go. I am fortunate in controlling the source for these services. It would be more difficult were that not the case. Thanks for your input.
RichieACC
This is good idea. Unahppily there exists webservicecs with bizzare behaviour (it's mainly cause the webservice are poorly implemented with some non-standard way) which returns by default Http errors ( I met some ). But in commonly case, is your solution better than the mine.
TcKs
A: 

The tip: create a interface/baseclass with method "InvokeWithSomeParameters". The meaning of "SomeParameters" should be a "parameters which 100% does not affect any important state".

I think, there are 2 cases:

  • Simple webservice, which does not affect any data on server. For example: GetCurrentTime(). This web service can be called without parameters.
  • Complex webservice, which can affect some daty on server. For example: Enlist pending tasks. You fill-up parameter with values which 100% throws a exception (resp. does not change affect pending tasks), if you got some exception like "ArgumentException", you know the service is alive.

I don't think, this is most clear solution, but it should works.

TcKs
A: 

How about opening a TCP/IP connection to the port used by the webservice? If the connection works, the RAS connection, the rest of the network and the host are all working. The webservice is almost certainly running too.

Thomas Bratt
afaik, the default for the webservice is to run on port 80, the same as websites. Testing communication to the port would confirm that the webserver is running, not that the webservice is available.
RichieACC
+3  A: 

You could try the following which tests the web site's existence:

public static bool ServiceExists(string url, bool throwExceptions, out string errorMessage)
{
    try
    {
        errorMessage = string.Empty;

        // try accessing the web service directly via it's URL
        HttpWebRequest request = WebRequest.Create(url) as HttpWebRequest;
        request.Timeout = 30000;

        using (HttpWebResponse response = request.GetResponse() as HttpWebResponse)
        {
            if (response.StatusCode != HttpStatusCode.OK)
                throw new Exception("Error locating web service");
        }

        // try getting the WSDL?
        // asmx lets you put "?wsdl" to make sure the URL is a web service
        // could parse and validate WSDL here

    }
    catch (WebException ex)
    {   
        // decompose 400- codes here if you like
        errorMessage = string.Format("Error testing connection to web service at \"{0}\":\r\n{1}", url, ex);
        Trace.TraceError(errorMessage);
        if (throwExceptions)
            throw new Exception(errorMessage, ex);
    }   
    catch (Exception ex)
    {
        errorMessage = string.Format("Error testing connection to web service at \"{0}\":\r\n{1}", url, ex);
        Trace.TraceError(errorMessage);
       if (throwExceptions)
            throw new Exception(errorMessage, ex);
        return false;
    }

    return true;
}
Oplopanax