title: How can I configure NServiceBus with multiple StructureMap child containers?
body: <p>I have a service with handlers from several different domains. Each domain is self-contained, and I would like to use a StructureMap <a href="http://structuremap.github.io/the-container/profiles-and-child-containers/" rel="nofollow">child container</a> for each domain to segregate certain dependencies (notably, Entity Framework's DbContext). Note that what NServiceBus calls a child container is what StructureMap would call a <a href="http://structuremap.github.io/the-container/nested-containers/" rel="nofollow">nested container</a>; nested containers manage lifecycles (typically on a per-request basis) while child containers allow different domains to share dependencies registered in the parent container while registering overriding or conflicting dependencies in the child container.</p>
  
  <p><strong>Is there any way I can have this single service properly register each of its child containers with NServiceBus?</strong></p>
  
  <p>A workaround I've seen elsewhere is to use a single container and avoid registering conflicting dependencies at startup. Instead, the transient container (NServiceBus' "child" container / StructureMap's "nested" container) is injected directly into the handler, which registers whatever dependencies it needs. I would really like to avoid that approach if possible, however, as locating the appropriate dependencies requires a relatively expensive scan, and I'd like to do the scan once when the service is started rather than every time an event or command is received.</p>
  
  <p>I would also prefer to keep all the handlers in the same service. I already have a large number of domains and expect to add more later, so building one service per domain would entail creating or licensing an architecture to manage all the resulting services. None of the individual domains receives very many messages; our testing suggests a single service should be able to handle all of their workloads.</p>