Windsor Dependency Injection Usage

Component: Castle Dependency Injection
NuGet Package NServiceBus.CastleWindsor (7-pre)
Target NServiceBus Version: 7.x
This page targets a pre-release version and is subject to change prior to the final release.

Configuring to use Windsor

var endpointConfiguration = new EndpointConfiguration("Samples.Castle");

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

endpointConfiguration.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 Task Handle(MyMessage message, IMessageHandlerContext context)
    {
        myService.WriteHello();
        return Task.CompletedTask;
    }
}

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