I\'ve set up Unity for dependency injection for our project. The project itself is an ASP.NET application that uses both MVC and Web API.
For the database context, I
The problem turned out to be that the UnityDependencyResolver
was caching the resolved items over several requests. I had to change it to the UnityHierarchicalDependencyResolver
and then it started resolving my items properly according to the associated LifetimeManager
. The problem initially became more confusing when it appeared that even when using a TransientLifetimeManager
, it would still return the same instance.
I found the answer in a different (yet somewhat related) question: using a Handler in Web API and having Unity resolve per request
So all I did was change
GlobalConfiguration.Configuration.DependencyResolver = new Microsoft.Practices.Unity.WebApi.UnityDependencyResolver(container);
to
GlobalConfiguration.Configuration.DependencyResolver = new Microsoft.Practices.Unity.WebApi.UnityHierarchicalDependencyResolver(container);
and all my problems were solved.