Search code examples
c#dependency-injectionnservicebusstructuremapnservicebus4

How do I update to StructureMap 3.x while staying on NServiceBus 4.x?


I want to upgrade to StructureMap 3.x on my MVC project, but I'm stuck on NServiceBus 4.x because the 5.x license terms do not make sense for our limited usage. The MVC project is send-only.

How can I upgrade StructureMap while leaving NSB 4.x in place?


Solution

  • Instead of this:

    Configure
        .With()
        .StructureMapBuilder(ObjectFactory.Container)
    

    Use the default container:

    Configure
        .With()
        .DefaultBuilder()
    

    Because you're no longer passing ObjectFactory.Container through to NServiceBus, you'll need to tell StructureMap how to fulfill IBus as a dependency for, e.g. your MVC controllers:

    x.For<NServiceBus.IBus>()
        .Use(() => Configure
            .Instance
            .CreateBus()
            .Start(() => Configure.Instance.ForInstallationOn<Windows>().Install())
        );
    

    If you have something that NServiceBus needs to create, e.g. something that implements IMutateOutgoingMessages and your message mutator has dependencies, then you'll have to tell NServiceBus how to resolve those dependencies:

    Configure
        .Instance
        .Configurer
        .ConfigureComponent(
            () => new MessageMutator(new MessageMutatorDependency()),
            DependencyLifecycle.InstancePerCall
        );