Archives

  • Categories

  • Getting Started With NServiceBus: Part 4 Integrating With StructureMap

    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:

               Configure.With()
                    .StructureMapBuilder(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)
            {
                container
                    .Model
                    .PluginTypes
                    .Where(p => p.Implements<INeedBuildUp>())
                    .Select(x => x.To<INeedBuildUp>(container))
                    .Foreach(container.BuildUp);
            }
    

    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)
                {
                    action(item);
                }
            }
    

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

    kick it on DotNetKicks.com

    6 Responses to “Getting Started With NServiceBus: Part 4 Integrating With StructureMap”


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

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

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

    4. Arsento says:

      Thank you! You often write very interesting articles. You improved my mood.

    5. Excellent article!

      Just a note:

      The structuremap builder should enable both setter and ctor injection out of the box. Can you verify that is doesn’t work for you? ( if not, what NServiceBus version are you using?)

    6. Stuart C says:

      Hi Andreas yes constructor and setter injection does work for me. Your comments have made me realize that I didn’t make it clear in my article why I use setter injection for the IBus instance which is because I like to unit test my StructureMap registries using the ObjectFactory.AssertConfigurationIsValid(). By using setter injection for the IBus it means I can test my registries in isolation without having to get NServiceBus involved.

    Leave a Reply