I have a ASP.Net MVC application set up with Structuremap and StructuremapDependencyScope
serving up a nested container for each request.
I would like to be able to build up a list and have StructureMap return that list, for as long as the custom lifecycle is valid.
I have the following in a Structuremap Registry
:
For<List<Cat>>()
.Use(x => x.GetInstance<CatlistBuilder>().Build())
.LifecycleIs<NineLifesLifecyle>();
This works great, the list List<Cat>
is built only once until the lifecycle expires.
However, the instance of CatlistBuilder
(and all the other dependencies it needs) is not disposed at the end of the request. I suspect the reason is the custom lifecycle - since the configuration uses a custom lifecycle, the IContext
passed into the lambda function seems to be the root container. If I remove the custom lifecycle, the current nested container is used.
Is it possible to have the nested container provide the CatlistBuilder
class, and still use a custom lifecycle for the result?
The only workaround I could come up with was to use
.Use(x => StructuremapMvc.StructureMapDependencyScope.CurrentNestedContainer.GetInstance<CatlistBuilder>().Build())
but that does not seem correct.