Spring Container Usage

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

Configuring NServiceBus to use the container

var endpointConfiguration = new EndpointConfiguration("Samples.Spring");
var applicationContext = new GenericApplicationContext();
applicationContext.ObjectFactory
    .RegisterSingleton("MyService", new MyService());
endpointConfiguration.UseContainer<SpringBuilder>(
    customizations: customizations =>
    {
        customizations.ExistingApplicationContext(applicationContext);
    });

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.
  • Spring
    Details on how to Configure NServiceBus to use Spring as a container. Includes usage examples as well as lifecycle mappings.

Last modified