由于 Rebus app.config XML 对每个进程一个总线实例的场景进行了优化,因此您无法完全在 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
(这是一种温莎机制)基本上只是将具有正确队列名称的总线放入容器中,例如:
Configure.With(new WindsorContainerAdapter(container))
.Transport(t => t.UseMsmq(_inputQueueName, _errorQueueName))
.(...) etc
.CreateBus().Start();
我喜欢每个 IoC 容器实例独立作为逻辑独立应用程序的想法。这样,如果您希望能够独立部署您的端点,那么将来某个时间就可以很容易地将事情分开。
我希望这能为您提供一些启发:) 请不要犹豫,询问您是否需要更多指示。