Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What exception or http status code for when the server is down

I want to monitor a WCF server, and send email notification if the server is down. To accomplish that, I am writing a console app to periodically send dummy request to the server, and check if response is sent back. When the console app received exception the server has issues, including the server being down.

However, the problem is that I received different exception on different status of the server. Below is the exceptions returned from the server when it is on different status. However, all seem belong to server down category. Any idea??:

When IIS is turned off

System.ServiceModel.EndpointNotFoundException,

Message:
There was no endpoint listening at http://localhost/service.svc that could accept the message. This is often caused by an incorrect address or SOAP action. See InnerException, if present, for more details.

Inner Exception Message:The remote server returned an error: (404) Not Found

When a Web.config file is deliberately changed to a wrong name:

System.ServiceModel.ServiceActivationException
Link: http://localhost/service.svc
Message:
The requested service, 'http://localhost/service.svc' could not be activated. See the server's diagnostic trace logs for more information.

For other unknown reason

System.ServiceModel.ServerTooBusyException
Message:
The HTTP service located at http://localhost/service.svc' is too busy.
Message:
The remote server returned an error: (503) Server Unavailable.

Update 1

The exception does NOT always return http status code.

Update 2 Apart from using WCF proxy to call the service, I have to use WebRequest too, as below:

       try
        {
            WebRequest webRequest = WebRequest.Create(uri);
            webRequest.Method = "GET";

            HttpWebResponse httpWebResponse = (HttpWebResponse)webRequest.GetResponse();                             

        }
        catch ()  //what excpetion will tell me server is down??
        {
           ...
        }
like image 736
Pingpong Avatar asked Oct 23 '22 10:10

Pingpong


2 Answers

The actual content of the error shouldn't really be of consequence - unless you're monitoring individual operations on the service (i.e. should a POST with some data to a particular URL return a specific response) - realistically, then, you're just going to be looking at the status code itself; and for that you want to look through all the HTTP Status Codes and see those which look like errors as far as you're concerned.

As a good starting point - you might want to consider nearly all of the 5xx codes; as they are all connected with server errors.

You might also want to consider some of the 4xx codes (although these are usually connected with clients, so be ruthless). In particular:

400 - Bad Request - so long as you can be sure that the server should be able to understand the request

404 - Not Found - if you're sure that the given URL should be present

405 - Method Not Allowed - if you're sure that the given HTTP verb should be supported (e.g. a POST or DELETE)

For some of the narrower 4xx codes, e.g. 413 Request Entity Too Large or 414 Request-URI Too Long; these could conceivably happen after days or months of normal operation due to things like security updates. In which case you're not necessarily identifying that the service is down as such, but you might be anticipating it being unable to perform it's intended function.

like image 164
Andras Zoltan Avatar answered Oct 26 '22 20:10

Andras Zoltan


Any HTTP status result code in the 400 or 500 series is a problem that will prevent you're request from processing. All of these errors derive from System.ServiceModel.CommunicationException so check for that.

like image 36
JamieSee Avatar answered Oct 26 '22 18:10

JamieSee