AutoFac / Unity Container – Dependency Injection with multiple implementations
I am going to work with Dependency Injection for a ASP.NET Web API project.
I am going to work with Dependency Injection for a ASP.NET Web API project.
A few days ago, I had an issue with ASP.Net threading. I wanted to have a singleton object per web request. I actually need this for my unit of work. I wanted to instantiate a unit of work per web request so that identity map is valid through out the request. This way I could use an IoC to inject my own IUnitOfWork to my repository classes transparently, and I could use the same instance to query and then update my entities.
I’m using Unity successfully for all regular constructor injection such as repositories etc., but I can’t get it working with the ASP.NET Identity classes. The setup is this:
My ASP.Net MVC 4 Web API controller doesn’t work with Unity.WebApi. In the same project simple controllers works with Unity.Mvc3 properly. But when I run Web API controller derived from ApiController I’m getting a message:
Does anyone have any good examples of how to make Unity 1.2 or 2.0 work with ASP.NET WebForms?
I need a way to store a logging object per request. With HttpContext I would add this to the items Dictionary. I don’t want to bring HttpContext into this if I can help it.
The below code is what I propose for a Unity LifeTimeManager that will store objects in OwinContext’s Environment property, which I have access to with my Owin middleware.