I'm looking to register an EasyNetQ's IBus
which implements IDisposable
as a singleton with StructureMap.
The issue is when using nested container, the instance is disposed with the container, thus leaving me with a singleton instance being disposed for every other containers.
For<IBus>()
.Use(c => BusBuilder.CreateMessageBus())
.Singleton();
ContainerScoped
is not an option here as I need that is kept alive during all the application lifespan.
How do I prevent StructureMap from disposing this instance with nested containers ? How do I tell it to dispose the instance only with the root container ?
I knew something was odd with my issue. What was the point of defining singletons if they were disposed with each container ?
Indeed, StructureMap does not dispose singleton objects unless a root container is being disposed, see the PipelineGrap.cs source code. :
if (Role == ContainerRole.Root)
{
_profiles.AllProfiles().Each(x => x.Dispose());
}
It turned out that my IBus
instance was being manually disposed by some mysterious legacy code in which it was injected.
Anyway, for anyone facing the same issue : track down your call to Dispose
as StructureMap is doing its job as you though it would :)