Project Description

Automatically creates a Unity container for each web request which is then properly disposed of when the request ends. An extension method makes the container accessible as HttpContext.Current.GetUnityContainer(). Works in ASP.NET Web Applications as well as MVC Applications. Written in C# 4.0.


Step 1: Add a reference to Unity 2.1 to your application. (Has not yet been tested with other versions of Unity.)

Step 2: Add a reference to G27.Web.PerRequestUnityContainer to your project.

Step 3: Add a using statement to the code from which you want to access the container. (REQUIRED, otherwise the HttpContext.Current.GetUnityContainer() extension won't be available.)

   using G27.Web.PerRequestUnityContainer;

Step 4: Register a class in the container:

    HttpContext.Current.GetUnityContainer().RegisterType<YourClass>(new ContainerControlledLifetimeManager());

Step 5: Resolve an object from the Container

   YourClass object = HttpContext.Current.GetUnityContainer().Resolve<YourClass>() as YourClass;

When the EndRequest event is fired the container will automatically be disposed of. Objects resolved from IDisposable classes and registered with a ContainerControlledLifetimeManager will also be properly disposed of.

Last edited Jul 26, 2011 at 1:17 AM by G27, version 20