问题描述
我正在尝试将解决方案从 NServiceBus 5 升级到 6.目前,我们正在将 IBus 注入服务类,以便服务可以根据需要向总线发送消息.现在 IBus 在 6 中不再可用,我如何在使用 NServiceBus.Host 创建端点时注入端点实例,它本身将启动此端点?
I'm trying to upgrade a solution from NServiceBus 5 to 6. Currently, we are injecting IBus to a service class so that the service can send messages to the bus as needed. Now that IBus is no longer available in 6, how do I inject the endpoint instance when the endpoint is created using NServiceBus.Host, which will itself start this endpoint?
https://docs.particular 的底部提供了一个示例.net/samples/hosting/multi-hosting/:
var endpointConfiguration = new EndpointConfiguration("Samples.MultiHosting.Instance1");
endpointConfiguration.UseSerialization<JsonSerializer>();
endpointConfiguration.EnableInstallers();
endpointConfiguration.UsePersistence<InMemoryPersistence>();
endpointConfiguration.SendFailedMessagesTo("error");
return Endpoint.Start(endpointConfiguration);
但是,紧随其后的是一条注释,表明这仅在自托管且不使用 NServiceBus.Host 时是可能的."
However, it is immediately followed by a note indicating "this is possible only when self-hosting and not using NServiceBus.Host."
推荐答案
使用 IWantToRunWhenEndpointStartsAndStops
实现在总线启动后为您的服务类注入 IMessageSession
.
Use an IWantToRunWhenEndpointStartsAndStops
implementation to inject the IMessageSession
for your service class after the bus has started.
以下是 NServiceBus.Host 升级指南中的相关部分:https://docs.particular.net/nservicebus/upgrades/host-6to7#iwanttorunwhenendpointstartsandstops-interface-in-version-7-of-nservicebus-host
Here is the relevant section from the Upgrade Guide for NServiceBus.Host:https://docs.particular.net/nservicebus/upgrades/host-6to7#iwanttorunwhenendpointstartsandstops-interface-in-version-7-of-nservicebus-host
另一种方法是切换到 NServiceBus Windows 服务引导程序 而不是使用 NServiceBus.Host,您可以在其中更好地控制端点的生命周期.
An alternative would be to switch to the NServiceBus Windows Service Bootstrap instead of using NServiceBus.Host where you will have more control over the endpoint's lifecycle.
这篇关于NServiceBus.Host - 如何将端点实例添加到我的 IoC 容器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!