1

We have a system, which is not public facing, that has to do a bunch of work when it goes down for an update, etc.

During this time, the REST interface can still receive requests and I'm looking for a global solution where any call would not be processed by the regular controllers but just return a unique code (for example a 503). I'd like to avoid something like "if (shutdown) ..." in each controller method :)

Is there a provision for something like this in the asp.net core api?

Thomas
  • 10,933
  • 14
  • 65
  • 136

1 Answers1

1

You may try to register IApplicationLifetime.ApplicationStopping action to catch up when shutdown is starting. But note that this is only for a graceful shutdown.

/// <summary>
/// Triggered when the application host is performing a graceful shutdown.
/// Requests may still be in flight. Shutdown will block until this event completes.
/// </summary>
CancellationToken ApplicationStopping { get; }

Look into SO Where can I log an ASP.NET Core app's start/stop/error events? for implementation sample.


Then you may add a simple middleware that stops the pipeline execution and returns 503 status while an app is stopping:

public void Configure(IApplicationBuilder app)
{
    app.Use(async (context, next) =>
        {
            bool yourFlag = ... ;// should be true if application topping;

            if (yourFlag)
            {
                context.Response.StatusCode = 503;
                // stop further pipeline execution
                return;
            }
            await next.Invoke();
        });
     ...
}
Set
  • 47,577
  • 22
  • 132
  • 150
  • We know when the app is shutting down, what I'm looking for is to get every single rest call to the app to return a 503 status during that time. – Thomas Nov 24 '17 at 14:27
  • @Thomas ok so you may add a simple middleware that will return a 503 status for all request at that time. And make sure that it is located at the beginning of the pipeline. I have updated post – Set Nov 24 '17 at 15:14