I have developed a background service which is deployed to IIS. In my ExecuteAsync, I have added a top-level try-catch in which I perform Environment.Exit based on whether the cancellation was requested or not.
However, when I go and stop the service in services.msc, I get a popup telling me the service shut down unexpectedly.
This is how my ExecuteAsync looks like:
protected override async Task ExecuteAsync(CancellationToken stoppingToken)
{
try
{
// Do stuff
}
catch (Exception ex) when (stoppingToken.IsCancellationRequested)
{
_logger.LogError(ex, ex.Message);
Environment.Exit(0);
}
catch (Exception ex) when (!stoppingToken.IsCancellationRequested)
{
_logger.LogCritical(ex, ex.Message);
Environment.Exit(-1);
}
}
Is there a reason why I am receiving this popup when I shut down the service since the IsCancellationRequested should be set to true.
