ASP.NET Core Web API Interview Question #169

What is graceful shutdown in ASP.NET Core?

Testing, HttpClient, Integration Testing, Background Services & Deployment Senior Advanced

Quick Interview Answer

Graceful shutdown gives the application an opportunity to stop accepting work and allow in-flight operations or hosted services to finish or cancel safely before the process exits.

Detailed Explanation

Graceful shutdown gives the application an opportunity to stop accepting work and allow in-flight operations or hosted services to finish or cancel safely before the process exits.

Application code should observe cancellation tokens and avoid starting new long operations after shutdown begins.

Code Example

protected override async Task ExecuteAsync(
    CancellationToken stoppingToken)
{
    while (!stoppingToken.IsCancellationRequested)
    {
        await ProcessNextAsync(stoppingToken);
    }
}