2

I am using NServiceBus 4.0.3, StructureMap 2.6.4.0 and NHibernate

I have configured my endpoint as follows:

public class EndpointConfig : IConfigureThisEndpoint, 
    AsA_Client, IWantCustomInitialization
{
    public void Init()
    {
        Configure.With(
            .StructureMapBuilder(getContainer());

        Configure.Features.Disable<SecondLevelRetries>();
        Configure.Features.Disable<Sagas>();
        Configure.Features.Disable<TimeoutManager>();
    }
}

private IContainer getContainer()
{
    var container = new Container(x =>
    {

        x.Scan(s =>
        {
            s.WithDefaultConventions();
            s.TheCallingAssembly();
      });

        x.ForSingletonOf<ISessionFactory>().Use(createSessionFactory());
        x.For<ISession>().Use(context => 
            context.GetInstance<ISessionFactory>().OpenSession());
    });

    return container;
}

I have a pluginfamily and I want to get an instance of the object at runtime(cant use constructor injection).

In StructureMap world, it can be done by using:

ObjectFactory.GetNamedInstance<ISomething>("familyName");

But when I am using NServiceBus with structuremap, it gets a nested container for each message.
When I want to get an instance of an object, the above code will only give me an instance from the parent container. Since the ObjectFactory was never initialize, it doesn't get an instance.

Event if I initialize the ObjectFactory with my container, since it is a static wrapper it wont be safe to use it per message.

So how do I get handle on my child container for the given message so that I can use the plugin family?

Steven
  • 166,672
  • 24
  • 332
  • 435
user1414095
  • 113
  • 1
  • 5
  • This probably doesn't answer your question but could you not perhaps wrap the retrieval in some other object such as an implementation of an `ISomethingProvider`? The implementation could have access to the base instance of structure map that you require and it wraps the service locator bit. In your message handler you could then use constructor injection to get to the provider. I don't know structure map and haven't used the child containers of other tools so I hopefully I'm not talking too much nonsense :) – Eben Roux Dec 18 '13 at 04:08
  • I need to determine the instance at runtime so I do require a locator service but that determination of locator service is what I want to leave it up to Structuremap plugin family. – user1414095 Dec 18 '13 at 04:27

1 Answers1

1

You can't access the child through the NServiceBus IBuilder api (https://github.com/Particular/NServiceBus/issues/1565)

But I'm pretty sure you can get at it by injecting the native IContainer interface provided by StructureMap into you handler.

In short:

Take a dependency on StructureMap.IContainer

Andreas Öhlund
  • 5,263
  • 20
  • 24