由于 Rebus app.config XML 针对每进程一个总线实例的场景进行了优化,因此您无法配置多个总线fully在 XML 中,但是没有什么可以阻止您在同一进程内启动多个总线实例。
我经常这样做,例如在 Azure 辅助角色中,我希望托管多个逻辑端点,而不会产生物理上独立部署的成本,有时我也使用 Topshelf 托管的 Windows 服务来完成此任务。
通常,我的 app.config 最终会包含以下 XML:
<rebus workers="1">
<add messages="SomeAssembly.Messages" endpoint="someEndpoint.input"/>
<add messages="AnotherAssembly.Messages" endpoint="anotherEndpoint.input"/>
</rebus>
因此,我可以一劳永逸地配置每条总线的默认工作人员数量和端点映射。然后,当我的应用程序启动时,它将在应用程序生命周期内为每个总线保留一个 IoC 容器 - 使用 Windsor,我通常会得到一个通用总线安装程序,其中包含队列名称的参数,这允许我配置 Windsor像这样:
var containers = new List<IWindsorContainer> {
new WindsorContainer()
// always handlers first
.Install(FromAssembly.Containing<SomeEndpoint.SomeHandler>())
// and then the bus, which gets started at this point
.Install(new RebusInstaller("someEndpoint.input", "error"))
// and then e.g. background timers and other "living things"
.Install(new PeriodicTimersInstannce()),
new WindsorContainer()
.Install(FromAssembly.Containing<AnotherEndpoint.AnotherHandler>())
.Install(new RebusInstaller("anotherEndpoint.input", "error"))
};
// and then remember to dispose each container when shutting down the process
哪里的RebusInstaller
(这是一种 Windsor 机制)基本上只是将具有正确队列名称的总线放入容器中,例如像这样:
Configure.With(new WindsorContainerAdapter(container))
.Transport(t => t.UseMsmq(_inputQueueName, _errorQueueName))
.(...) etc
.CreateBus().Start();
我喜欢这样的想法:每个 IoC 容器实例都可以作为逻辑上独立的应用程序运行。这样,如果您想在将来的某个时候将事情分开,例如能够独立部署您的端点。
我希望这能为您提供一些灵感:)如果您需要更多指导,请随时询问。