Getting Started With NServiceBus: Part 4 Integrating With StructureMap « Plan B Software


: 3

Here are the links to the previous posts in this series:

Part 1 Unicast Messaging.
Part 2 Publishing and Subscribing.
Part 3 Sagas
Part 4 Integrating With StructureMap

Out of the box NServiceBus uses the Spring.Net framework IOC container. However if you want to integrate NServiceBus into your application this does not mean that you are limited to choosing Spring.Net as your container as NServiceBus allows you to plugin your IOC container of choice.

Currently  the Beta 2 version supports the following IOC containers.

  • Unity
  • Structuremap
  • Castle
  • Autofac

In this example I will walk you through the steps to get NServiceBus working with StructureMap.

The first thing  you must do is reference the NServiceBus.ObjectBuilder.StructureMap.dll.  If you download NServiceBus you will find this assembly in the \binaries\containers\structuremap folder. Once you have done this you can initialize your StructureMap container in pretty much the same way as you normally would. Once you have done that you can then initialize NServiceBus like this passing in the instance of your container:

                .... Rest of Configuration Goes Here......

If any of your classes need to access the Bus to publish or send a message an instance of IBus must be injected into your class by your IOC container. However if you try to use constructor injection on your container, StructureMap will complain that it does not have a default instance defined for IBus especially if you have unit tests over your registry that work in isolation of NServiceBus.

The best way to get round this is on the classes that need to interact with the bus to have a public property of type IBus and use property injection to inject our instance of the Bus once NServiceBus has been initialized.

In StructureMap there are a number of different approaches to doing property injection including marking up properties with StructureMap attributes, however the technique that I favour which I unashamedly stole from Jeremy Miller’s StoryTeller project works as follows:

On the class that you wish to access the Bus you add a public property of type IBus

 public IBus Bus { get; set; }

The class should also implement the empty marker interface INeedBuildUp

Now in our StructureMap registry we should set up a rule stating that any class that has a pubic property of type IBus should be set by our IOC container.

SetAllProperties(y => y.OfType<IBus>());

Now this on its own is not enough to inject our IBus instance into our classes as we need to call the IContainer.BuildUp() method passing in our instance of that class to inject in our IBus instance.

This is where the following little extension method comes in handy that can be run from your application bootstrapper once StructureMap and NServiceBus have been initialized, which queries the IOC container for any pluginTypes that implement the INeedBuildUp interface foreach instance that is returned the BuildUp method is called on the container.

 public static void BuildUpThoseThatImplementINeedBuildUp(this IContainer container)
                .Where(p => p.Implements<INeedBuildUp>())
                .Select(x => x.To<INeedBuildUp>(container))

Which in turn uses the following extension methods to do its magic..

public static bool Implements<T>(this PluginTypeConfiguration configuration)
            var implements = typeof (T).IsAssignableFrom(configuration.PluginType);

            return implements;

 public static T To<T>(this PluginTypeConfiguration configuration, IContainer container)
            return (T) container.GetInstance(configuration.PluginType);

public static void Foreach<T>(this IEnumerable<T&gt items, Action<T&gt action)
            foreach (var item in items)

Voila! you’ve now integrated StructureMap with NServiceBus in your application.

kick it on