rebus-org / Rebus.ServiceProvider

:bus: Microsoft Extensions Dependency Injection container adapter for Rebus
https://mookid.dk/category/rebus
Other
68 stars 32 forks source link

Why does AddRebus method throws Exception if called more than once. #13

Closed alexdrl closed 6 years ago

alexdrl commented 6 years ago

I've read the code of the 'AddRebus' method, and throws an InvalidOperationException if the IBus interface is already registered.

What is the reasoning behind this? Some .NET Core libraries allow this, without throwing exceptions.

Also, if the problem is about re-registering something, you could try to register only once with the TryAdd method...

mookid8000 commented 6 years ago

Rebus requires its container adapters to throw an InvalidOperationException if the bus is configured twice with that container.

The reason is that it automatically registers IBus, ISyncBus, and IMessageContext in containers, and it would lead to ambiguous code and hard to debug errors if you then simple had IBus injected into your class without specifying which bus.

Moreover, I have often experienced people thinking that they should register multiple buses e.g. in order to host multiple endpoints within the same process, but if that's the goal, then it's better to simply new up multiple containers.