我正在尝试使用Rebus Core3.1和.NET执行正常关闭,但是当我试图在IHostedService中注入Rebus时,它会停止记录信息。
我的设置如下
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." });
}
}在我的初创公司里,我有
services.AddHostedService<LifetimeEventsHostedService>();发布于 2020-07-14 01:05:46
当您使用任何Rebus的IoC集成包(Rebus.ServiceProvider、Rebus.CastleWindsor、Rebus.Autofac等)时,它将自动设置自身,以便在容器被释放时正常关闭。
因此,如果您希望总线正常关闭,只需确保它所在的IoC容器在应用程序关闭时得到释放。
这也适用于内置容器适配器(它实际上不是IoC容器,更像是某种处理程序工厂)-只需执行以下操作(或类似的操作):
using (var activator = new BuiltinHandlerActivator())
{
Configure.With(activator)
.Transport(...)
.(...)
.Start();
// bus is running now
}
// bus is stopped – all handlers have finished executing然后总线将优雅地关闭,停止接收操作,给所有处理程序最多60秒(如果我没记错的话)来完成它们正在做的任何事情。
https://stackoverflow.com/questions/62878788
复制相似问题