[ad_1]
I have a basic ASP.NET application hosted as an Azure linux app service and I want to track when the service is Starting, Started, Stopping and Stopped. This works great for Starting and Started but when it comes to Stopping and Stopped it doesn’t appear that the linux app service calls the necessary OnStopping and OnStopped methods.
internal class LifetimeEventsHostedService : IHostedService
{
private readonly ILogger _logger;
private readonly IHostApplicationLifetime _appLifetime;
public LifetimeEventsHostedService(
ILogger<LifetimeEventsHostedService> logger,
IHostApplicationLifetime appLifetime)
{
_logger = logger;
_appLifetime = appLifetime;
}
public Task StartAsync(CancellationToken cancellationToken)
{
_logger.LogInformation("***Starting Service Async");
_appLifetime.ApplicationStarted.Register(OnStarted);
_appLifetime.ApplicationStopping.Register(OnStopping);
_appLifetime.ApplicationStopped.Register(OnStopped);
return Task.CompletedTask;
}
public Task StopAsync(CancellationToken cancellationToken)
{
_logger.LogInformation("***Stopping Service Async");
return Task.CompletedTask;
}
private void OnStarted()
{
_logger.LogInformation("***Starting Service. OnStarted has been called.");
}
private void OnStopping()
{
_logger.LogInformation("***Stopping Service. OnStopping has been called.");
}
private void OnStopped()
{
_logger.LogInformation("***Stopped Service. OnStopped has been called.");
}
}
The above code works fine for Starting and Started but not for Stopping and Stopped. It also works fine in a Windows App Service. Is this a bug with Linux App Services? Is there anyway to circumvent this?
[ad_2]