StructureMap Container Usage

Component: StructureMap Container
NuGet Package NServiceBus.StructureMap (6.x)
Target NServiceBus Version: 6.x

Configuring NServiceBus to use the container

var endpointConfiguration = new EndpointConfiguration("Samples.StructureMap");
var container = new Container(
    action: expression =>
    {
        expression.For<MyService>()
            .Use(new MyService());
    });
endpointConfiguration.UseContainer<StructureMapBuilder>(
    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

  • Containers
    NServiceBus automatically registers components, handlers, and sagas.
  • StructureMap
    Details on how to Configure NServiceBus to use StructureMap as a container. Includes usage examples as well as lifecycle mappings.

Last modified