Per Request ChildContainer for MVC - Where to Dispose?

Nov 15, 2008 at 12:34 AM

I really like the idea you mentioned in an earlier thread about creating a child container for per request items and creating the container in BeginRequest and disposing of it in EndRequest.

That works for Webforms, but how would you do this in the MVC Framework?

I assume you would create the child container and register the types in the ControllerFactory within the GetControllerInstance Method right before resolving the controller, but where would you make the call to dispose of the Child Container?


Nov 15, 2008 at 1:18 AM
Stick the child container in HttpContext.Current.Items, the dispose of it in the DisposeController method of the Controller factory most likely.

Or you could just leave the dispose call in EndRequest - those events still fire, MVC or not.
Mar 31, 2011 at 1:12 PM
Edited Apr 1, 2011 at 9:04 AM

If you have upgraded to ASP.NET MVC 3, you might like to try which takes care of all this for you. It is also available as a NuGet package (Unity.Mvc3)