.net core 优美关机

.net core 优美关机,.net-core,asp.net-core-3.1,rebus,.net Core,Asp.net Core 3.1,Rebus,我正在尝试使用.NET Core 3.1和Rebus执行优雅的关机,但是当我尝试在IHostedService中注入Rebus时,它会停止记录信息 我的设置如下 internal class LifetimeEventsHostedService : IHostedService { private readonly ILoggingAdapter<LifetimeEventsHostedService> _logger; private re

我正在尝试使用.NET Core 3.1和Rebus执行优雅的关机,但是当我尝试在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." });
        }
    }
内部类LifetimeEventsHostedService:IHostedService
{
专用只读iLogginAdapter\u记录器;
私有只读IHostApplicationLifetime\u ApplyTime;
专用只读IBus_总线;
公共生活事件支持服务(
ILoggingAdapter记录器,
IHostApplicationLifetime应用时间,
IBus总线)
{
_记录器=记录器;
_appLifetime=appLifetime;
_总线=总线;
}
公共任务StartSync(CancellationToken CancellationToken)
{
_appLifetime.ApplicationStarted.Register(OnStarted);
_appLifetime.ApplicationStopping.Register(OnStopping);
_appLifetime.ApplicationStopped.Register(OnStopped);
返回Task.CompletedTask;
}
公共任务StopAsync(CancellationToken CancellationToken)
{
返回Task.CompletedTask;
}
私有void OnStarted()
{
_logger.LogWarning(新登录项{Message=“应用程序已启动。”});
}
私有void OnStopping()
{
_logger.LogInformation(新的LogItem{Message=“Application Stopping.”);
_总线.Advanced.Workers.SetNumberOfWorkers(0);
}
私有空间
{
_logger.LogInformation(新登录项{Message=“应用程序已停止。”});
}
}
在我的创业公司里,我有

services.AddHostedService<LifetimeEventsHostedService>();
services.AddHostedService();

当您使用任何Rebus的IoC集成软件包(Rebus.ServiceProvider、Rebus.CastleWindsor、Rebus.Autofac等)时,它将自动设置,以便在处置容器时正常关闭

因此,如果您希望总线正常关闭,只需确保在应用程序关闭时,它所在的IoC容器已被释放

这也适用于内置容器适配器(实际上不是IoC容器,更像是某种处理器工厂)——只需执行以下操作(或类似操作):

使用(var activator=new builtinhandlerator())
{
使用(激活器)配置
.运输(……)
.(...)
.Start();
//公共汽车正在行驶
}
//总线已停止–所有处理程序都已完成执行
然后总线将优雅地关闭,停止接收操作,给所有处理程序60秒的时间(如果我没记错的话)来完成他们正在做的事情