NServiceBus.Host - How do I add the endpoint instance to my IoC container

我怕爱的太早我们不能终老 提交于 2019-12-02 11:14:12

问题


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?

There is an example provided at the bottom of 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);

However, it is immediately followed by a note indicating "this is possible only when self-hosting and not using NServiceBus.Host."


回答1:


Use an IWantToRunWhenEndpointStartsAndStops implementation to inject the IMessageSession for your service class after the bus has started.

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

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.



来源:https://stackoverflow.com/questions/40347980/nservicebus-host-how-do-i-add-the-endpoint-instance-to-my-ioc-container

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!