Thursday, March 31, 2011

How to check if a web service is available using .NET

If a .NET application needs to call a web service is there a good solution for checking if the web service is currently up and available, outside of calling a specific web method?

On my web services I usually include some sort of status method for testing, but this becomes an issue when using a third party web service.

From stackoverflow
  • You would need to call a method regardless, otherwise you wouldn't know if the web service itself was running. I.e. The server could be running but the web service stopped.

    We do this for some of our web services and it exposes neat functionality. We can have it return true/false and a failed call or false means its down. This lets you "return true;" always or conditionally return false if you choose to, such as blocking specific clients or turning the service off without actually stopping the web service, etc.

  • You could always do a simple ping to your destination to see if you get a response pack. Microsoft has a simple way of doing this outlined here.

    Granted this won't verify if the actual service is up and running, but it would save the overhead of preparing a full fledged service call.

    Allen : You would only save milliseconds and it would not be something you could trust.
  • Nope.

    One thing you might do is create a ServiceIsUp method that does nothing, only returning true. If your server was hung for whatever reason, your request would timeout.

  • Another solution is just to do a simple HTTP get on the URL of the service. And look at the response code (i.e. 404 etc). The cool thing about this is that you don't have to post any data or call any methods.

    Allen : If you stopped the web service, would this work? Isn't an error page shown instead of a 404?
    James : Technically if they responded with some general html stating the app is down you would be correct. So what you can do is append "?WSDL=" to the query string of the URL. If you don't get valid XML back you know for sure it's down. Most Web Service technologies support this semantic.
    James : Edit just "?WSDL" ignore the "="

0 comments:

Post a Comment