Windsor Dependency Injection Usage

Component: Castle Dependency Injection
NuGet Package NServiceBus.CastleWindsor (5.x)
Target NServiceBus Version: 5.x

Configuring to use Windsor

var busConfiguration = new BusConfiguration();
busConfiguration.EndpointName("Samples.Castle");

var container = new WindsorContainer();
var registration = Component.For<MyService>()
    .Instance(new MyService());
container.Register(registration);

busConfiguration.UseContainer<WindsorBuilder>(
    customizations: customizations =>
    {
        customizations.ExistingContainer(container);
    });

Injecting the dependency in the handler

public class MyHandler :
    IHandleMessages<MyMessage>
{
    MyService myService;

    public MyHandler(MyService myService)
    {
        this.myService = myService;
    }

    public void Handle(MyMessage message)
    {
        myService.WriteHello();
    }
}

Related Articles

  • Castle Windsor
    Details on how to Configure NServiceBus to use Castle Windsor for dependency injection. Includes usage examples as well as lifecycle mappings.
  • Dependency Injection
    NServiceBus automatically registers components, handlers, and sagas.

Last modified