NServiceBus can be hosted in any web technology that supports .NET, such as:
As most web technologies operate in a scale-out manner, NServiceBus is usually hosted in a "Send-only" manner. In this mode, the web application technology acts as a "forwarder" of messages rather than the "processor". The handling code (MVC controller, NancyFX module etc) of a given web request simply leverages the send APIs and no processing is done in the web process. The message handling is done in a Windows Service Endpoint.
In a web-hosted scenario, an IIS Recycle causes the hosted NServiceBus endpoint to shutdown and be restarted.
Web request handlers (MVC Controllers, WCF Handlers, NancyFx Modules, etc.) require access to the endpoint messaging session in order to send messages as a result of incoming HTTP requests. Many of the supported web application hosts resolve these web request handlers using dependency injection. NServiceBus already creates and manages its own dependency injection.
It seems reasonable to re-use the same dependency injection for both tasks but this will not work as the endpoint messaging session is not registered in the NServiceBus managed dependency injection. This is to prevent NServiceBus message handlers from taking a dependency on the endpoint messaging session. See Moving away from IBus - Dependency Injection for more info.
The recommended approach to handle this is to have two dependency injection instances: one for the web host and its web request handlers, and another for the NServiceBus endpoint and its message handlers. There are some things to consider when adopting this approach.
- Any service which is registered with the NServiceBus dependency injection will be available to be injected into NServiceBus message handlers but not web request handlers.
- Any service which is registered with the web host dependency injection will be available to be injected into web request handlers but not NServiceBus message handlers.
- Any service which is registered with both dependency injection instances can be resolved in both web request handlers and NServiceBus message bus handlers but:
- these will be different objects with different lifetimes.
- even if the services are registered with a singleton lifetime, there will still be one created for each dependency injection instance
- if a service needs to be shared and a single instance, it must be created externally during the web application host startup, and that specific instance must be registered in both dependency injection instances.