gpt4 book ai didi

.net-core - Rebus 优雅关机

转载 作者:行者123 更新时间:2023-12-04 09:30:12 26 4
gpt4 key购买 nike

我正在尝试使用 .NET Core 3.1 和 Rebus 执行正常关闭,但是当我尝试在 IHostedService 中注入(inject) 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>();

最佳答案

当您使用任何 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 秒(如果我没记错的话)来完成他们正在做的任何事情。

关于.net-core - Rebus 优雅关机,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/62878788/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com