I am using Structuremap as my Dependency Resolver. I am trying to implement Container Per Request Pattern on my Global.asax.cs file.
public IContainer Container
{
get
{
return (IContainer)HttpContext.Current.Items["_Container"];
}
set
{
HttpContext.Current.Items["_Container"] = value;
}
}
public void Application_BeginRequest()
{
Container = ObjectFactory.Container.GetNestedContainer();
}
As the ObjectFactory will not be supported in the future versions of Structuremap I would like get access to the container from the DependencyResolver. How is possible?
Thanks in Advance.
Noufal
I just tried this and its working, please let me if it is not the best way.
There are two dependency resolvers one for ASP.NET MVC and other for ASP.NET Web Api
Web Api: Use WebApiContrib.IoC.StructureMap.StructureMapResolver
MVC: Use StructureMapDependencyResolver
Usage is as follows...
Having ran into this question myself, this was the best guide I could find for registering StructureMap with ASP.NET MVC's Dependency Resolver (via the CommonServiceLocator package).
I've copied and pasted the aforementioned article's solution, but I would recommend going through the benefits of this solution in the original article.
You can then set the resolver like so:
The great result of this type of configuration is you receive a new child container per request, with the container being disposed of at the end of each request.