I'm trying to perform a graceful shutdown with .NET Core 3.1 and Rebus however when I try to inject Rebus in the IHostedService it stops logging info.
My setup is as follows
internal class LifetimeEventsHostedService : IHostedService
{
private readonly ILoggingAdapter<LifetimeEventsHostedService> _logger;
private readonly IHostApplicationLifetime _appLifetime;
private readonly IBus _bus;
public LifetimeEventsHostedService(
ILoggingAdapter<LifetimeEventsHostedService> logger,
IHostApplicationLifetime appLifetime,
IBus bus)
{
_logger = logger;
_appLifetime = appLifetime;
_bus = bus;
}
public Task StartAsync(CancellationToken cancellationToken)
{
_appLifetime.ApplicationStarted.Register(OnStarted);
_appLifetime.ApplicationStopping.Register(OnStopping);
_appLifetime.ApplicationStopped.Register(OnStopped);
return Task.CompletedTask;
}
public Task StopAsync(CancellationToken cancellationToken)
{
return Task.CompletedTask;
}
private void OnStarted()
{
_logger.LogWarning(new LogItem { Message = "Application Started." });
}
private void OnStopping()
{
_logger.LogInformation(new LogItem { Message = "Application Stopping." });
_bus.Advanced.Workers.SetNumberOfWorkers(0);
}
private void OnStopped()
{
_logger.LogInformation(new LogItem { Message = "Application Stopped." });
}
}
And then in my startup I have
services.AddHostedService<LifetimeEventsHostedService>();