>NServiceBus support for StructureMap

>

I’ve just committed support for StructureMap to the NServiceBus trunk so if you are using NSB and prefer StructureMap as your IoC-Container you can get the latest NSB build from teamcity.codebetter.com (thanks Udi for trusting me as a committer)

To configure NSB to use StructureMap use the following syntax:

var bus = Configure.With()
   .StructureMapBuilder()
   .MsmqSubscriptionStorage()
   .XmlSerializer()
   .MsmqTransport()
       .IsTransactional(true)
       .PurgeOnStartup(false)
   .UnicastBus()
       .ImpersonateSender(false)
   .CreateBus()
   .Start();

If you are using an explicit instance of the container it can be passed to the builder like this:

var container = new Container();
 
var bus = Configure.With()
   .StructureMapBuilder(container)
   ...

The bus itself is added to the container by NSB so to get access to is in your code just add a dependency for it the usual way:

public class ClassThatNeedsAccessToTheBus
{
    private readonly IBus bus;
 
    public ClassThatNeedsAccessToTheBus(IBus bus)
    {
        this.bus = bus;
    }
    public void PerformSomeLogic()
    {
        bus.Publish(new WhatEverEvent());
    }
}

And to fill your message handlers with your own dependencies just configure them in SM and add them as constructor or setter dependencies :

public class MyMessageHandler:IMessageHandler<WhatEverEvent>
{
    private IRepository repository;
 
    public MyMessageHandler(IRepository repository)
    {
        this.repository = repository;
    }
 
    public void Handle(WhatEverEvent message)
    {
        repository.Save(message);
    }
}

Edit: You can also configure dependencies using NServiceBus own syntax , more on that here.

If you have any troubles, suggestions etc. let me know directly or post them to the NSB group.

This entry was posted in NServiceBus, StructureMap. Bookmark the permalink. Both comments and trackbacks are currently closed.