dotnet core IWebHost with BackgroundService keeps running after CTRL+C

Viewed 512

I've created a dotnet core 2.1 app running MVC, and also added a BackgroundService (services.AddHostedService()).

It works fine, however quite often when i CTRL+C, the app exits, but the background service still runs & outputs debug to the console.

I've tried logging the state of the cancellation token, but the background service quite often doesn't get cancelled:

protected async override Task ExecuteAsync(CancellationToken cancellationToken)
{
    while (!cancellationToken.IsCancellationRequested)
    {
        Console.WriteLine(cancellationToken.IsCancellationRequested);
        await doWork(cancellationToken)
    }
}

the console output remains false...

Anyone know why the cancellation token isn't passed to the background task?

to run my app, I'm executing dotnet run from the command line

0 Answers
Related