Unity PerRequestLifetimeManager re-using object in different requests

前端 未结 1 1000
日久生厌
日久生厌 2021-01-02 14:46

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

相关标签:
1条回答
  • 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.

    0 讨论(0)
提交回复
热议问题