-1

I need to call a web service every 5 minutes which tells if the system down. The problem is if the server is down, the system does not call the controller. We are using attribute routing. If the server is down I need to create a hardcoded response but since it does not go to the controller, I cannot hardcode the response.

How do I call the controller even if there is exception.

jelliaes
  • 485
  • 5
  • 18
  • 1
    As you've probably figured out by now, you can't rely on the server to tell you if it's down if the server is down. Instead, call your web service, and if it doesn't respond, it is down. – Robert Harvey Nov 18 '19 at 02:20
  • @RobertHarvey there is an exception returned. But instead of returning the response, I want to return a hardcoded json response. Is this possible to do on controller when I am using attribute routing? – jelliaes Nov 18 '19 at 02:26
  • Use a separate proxy in front of your real service that forwards requests to it but returns your desired error response when the real service isn’t responding? That said this is a pretty odd thing to do, well-written clients to a service should be able to deal with detecting if a service is down themselves without expecting a specific response – millimoose Nov 18 '19 at 02:41
  • if the server is down, how on earth are you going to return anything from it? – Andrei Dragotoniu Nov 18 '19 at 12:50
  • @AndreiDragotoniu yes but the specs says I need to return hardcoded json response when it cannot contact the server – jelliaes Nov 19 '19 at 02:09
  • the specs can say the server needs to be on the moon, doesn't mean it will actually happen. that spec is either completely wrong or completely misunderstood. i suggest you get more details on what they are looking for – Andrei Dragotoniu Nov 19 '19 at 11:10

1 Answers1

0

A. If the system is down it cannot generate a response. But that's a good thing, otherwise you would not be able to detect if it's down or not.

B. If there's an exception the client will still get a response (500 for example) which tells it that the server is up (but with problems).

C. On the controller level I think this is the best you can do:

[Route("/alive")]
[AllowAnonymous]
[HttpGet]
public string Alive()
{
   return "I'm alive and loving it!";
}

D. For setting up custom error response for errors please see Global Error Handling in ASP.NET Web API 2 or Exception Handling in ASP.NET Web API's Exception Filters:

public static class WebApiConfig
{
    public static void Register(HttpConfiguration config)
    {
        config.Filters.Add(new ProductStore.NotImplExceptionFilterAttribute());

        // Other configuration code...
    }
}
tymtam
  • 31,798
  • 8
  • 86
  • 126
  • It cannot go to controller because it cannot find the route since the server is down. – jelliaes Nov 18 '19 at 02:25
  • You're going to have to define what you mean by "cannot find the route". If your route definitions aren't working, then in what way is it meaningful to claim that your server is alive? Or is this really a javascript / httpclient question about handling server errors? – Jeremy Lakeman Nov 18 '19 at 02:37