Getting Started

Web application hosting

NServiceBus can be integrated into any web technology that supports .NET, such as:

As most web technologies operate in a scale-out manner, NServiceBus can be hosted in a "Send-only" configuration. 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 NServiceBus send APIs and the messages are processed by a backend service.

Dependency injection integration

Using the Generic Host

NServiceBus can be easily integrated into any web host that supports the Microsoft Generic Host using the NServiceBus Generic Host integration. The Generic Host integration automatically registers IMessageSession with the dependency injection container.


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 when using self-hosting.

The recommended approach to handle this scenario 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 can be injected into NServiceBus message handlers but not the web request handlers.
  • Any service which is registered with the web host dependency injection mechanism can be injected into web request handlers but not the 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 object instances with different lifetimes.
    • even if the services are registered with a singleton lifetime, there will still be one created for each dependency injection instance unless the instance is explicitly managed by the caller.
    • if a service must 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.

Endpoint lifecycle

In a web-hosted scenario, recycling an IIS process causes the hosted NServiceBus endpoint to shutdown and restart.


Related Articles