SimpleInjector Dependency Injection Usage

Component: SimpleInjector Dependency Injection
NuGet Package NServiceBus.SimpleInjector (2.x)
This is a community run project
Target NServiceBus Version: 6.x

Configuring to use SimpleInjector

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

endpointConfiguration.RegisterComponents(
    registration: components =>
    {
        components.RegisterSingleton(new MyService());
    });
endpointConfiguration.UseContainer<SimpleInjectorBuilder>();

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


Last modified