How do I (gracefully) shut down a worker service from within itself?
In ASP.NET Core, background services are independent from the application. It's possible, e.g., for services to finish and yet the application continues executing. If you want your application to exit when your background service finishes, then you'll need to wire that up yourself.
You can inject IHostApplicationLifetime
into your service and then call IHostApplicationLifetime.StopApplication
. Something like this:
public sealed class MyService : BackgroundService
{
private readonly IHostApplicationLifetime _hostApplicationLifetime;
private readonly ILogger<MyService> _logger;
public MyService(IHostApplicationLifetime hostApplicationLifetime, ILogger<MyService> logger)
{
_hostApplicationLifetime = hostApplicationLifetime;
_logger = logger;
}
protected override async Task ExecuteAsync(CancellationToken stoppingToken)
{
try
{
while (!stoppingToken.IsCancellationRequested)
{
try
{
_logger.LogInformation("Worker running at: {time}", DateTimeOffset.Now);
await SomeMethodThatDoesTheWork(stoppingToken);
}
catch (Exception ex)
{
_logger.LogError(ex, "Global exception occurred. Will resume in a moment.");
}
await Task.Delay(TimeSpan.FromSeconds(10), stoppingToken);
}
}
finally
{
_logger.LogCritical("Exiting application...");
_hostApplicationLifetime.StopApplication();
}
}
}