Getting Started
Architecture
NServiceBus
Transports
Persistence
ServiceInsight
ServicePulse
ServiceControl
Monitoring
Samples

StructureMap Dependency Injection Usage

Configuring an endpoint to use StructureMap

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

var registry = new Registry();
registry.For<MyService>().Use(new MyService());

endpointConfiguration.UseContainer(new StructureMapServiceProviderFactory(registry));

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

  • Dependency Injection
    NServiceBus automatically registers components, handlers, and sagas.
  • StructureMap
    Details on how to Configure NServiceBus to use StructureMap for dependency injection.