![](/img/trans.png)
[英]Why a simple configuration in MassTransit creates 2 queues and 3 exchanges?
[英]DI configuration for multiple RabbitMQ exchanges and queues
我正在嘗試為EventBusRabbitMQ實現配置DI。 對於單個交換,隊列,它工作得很好。
services.AddSingleton<IEventBus, EventBusRabbitMQ>(serviceProvider =>
{
...
return new EventBusRabbitMQ(connection, "exchange_EX1", "queue_Q1",..);
});
並在應用程序配置中
var eventBus = app.ApplicationServices.GetRequiredService<IEventBus>();
eventBus.Subscribe<FooEvent, FooEventHandler>;
我想用EventBusRabbitMQ的不同配置注冊多個實現,以便在我解決IEventBus時可以選擇要選擇的交換和隊列。
我所不希望的是明確實現,因為唯一不同的只是交換和隊列。
services.AddSingleton<IEventBus, EventBusRabbitMQ_EX1_Q1>
services.AddSingleton<IEventBus, EventBusRabbitMQ_EX2_Q2>
我有什么替代品?
我認為,如果您有一套有限的實現,最好的解決方案就是考慮這樣的解決方案:
public interface IEventBusRabbitMQ_EX1_Q1:IEventBus
{
}
public interface IEventBusRabbitMQ_EX2_Q2:IEventBus
{
}
然后更改您的代碼以注入正確的實例
services.AddSingleton<IEventBusRabbitMQ_EX1_Q1, EventBusRabbitMQ_EX1_Q1>
services.AddSingleton< IEventBusRabbitMQ_EX2_Q2, EventBusRabbitMQ_EX2_Q2>
但是這里描述了另一種解決方案
基本上是一種更豐富的模式,但我不建議這樣做,因為它會降低代碼的可讀性。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.