Unity PerRequestLifetimeManager在不同请求中重用对象 [英] Unity PerRequestLifetimeManager re-using object in different requests

查看:168
本文介绍了Unity PerRequestLifetimeManager在不同请求中重用对象的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经为我们的项目设置了Unity以进行依赖项注入.该项目本身是同时使用MVC和Web API的ASP.NET应用程序.

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.

对于数据库上下文,我使用的是PerRequestLifetimeManager.这样做是为了使业务逻辑的不同部分使用相同的上下文(并因此使用相同的事务).

For the database context, I'm using the PerRequestLifetimeManager. This is done so that the different bits of business logic are using the same context (and thus the same transaction).

为了能够使用PerRequestLifetimeManager,我添加了对nuget程序包的引用 ASP.NET MVC的Unity bootstrapper 和ASP.NET Web API的 Unity bootstrapper .

In order to be able to use the PerRequestLifetimeManager, I've added references to the nuget packages Unity bootstrapper for ASP.NET MVC and Unity bootstrapper for ASP.NET Web API.

要在Web API中使用此生存期管理器,请将以下行添加到启动代码中:

For use of this lifetime manager in Web API, the following line has been added to the startup code:

Microsoft.Web.Infrastructure.DynamicModuleHelper.DynamicModuleUtility.RegisterModule(typeof(UnityPerRequestHttpModule));

为MVC和Web API设置了Unity容器:

The Unity container is set up for both MVC and Web API:

var container = BuildUnityContainer();
GlobalConfiguration.Configuration.DependencyResolver = new Microsoft.Practices.Unity.WebApi.UnityDependencyResolver(container);
System.Web.Mvc.DependencyResolver.SetResolver(new Microsoft.Practices.Unity.Mvc.UnityDependencyResolver(container));

在构建Unity容器时,将数据库上下文设置为按以下方式针对每个请求进行解析:

In building the Unity container, the database context is set up to be resolved per request in the following way:

container.RegisterType<IDataContext>(new PerRequestLifetimeManager(),
    new InjectionFactory(c =>
    {
        // Some code
        return new DataContext(/* params */);
    }
));

但是,似乎此代码并未为每个请求提供新的DataContext.它在单个请求中的不同位置为我提供了相同的上下文(很好).但是,随后的(Web API)请求被赋予相同的DataContext实例,在该实例中,我希望为每个新请求创建一个新实例.我还希望DataContext在请求完成后得到正确处理(该类实现了IDisposable).

However, it seems that this code is not giving me a new DataContext for each request. It is giving me the same context in different places within a single request (which is fine). However, subsequent (web api) requests are being given the same instance of DataContext where I would expect a new one to be created for each new request. I also would expect the DataContext to be properly disposed of after the request is finished (the class implements IDisposable).

这是怎么回事?我是否缺少一些配置以使其正常工作?还是不应该按照我期望的方式工作?

What's going on here? Am I missing a bit of configuration to make this work properly? Or isn't this supposed to work the way I expect it to?

推荐答案

原来的问题是UnityDependencyResolver在多个请求上缓存了已解决的项目.我不得不将其更改为UnityHierarchicalDependencyResolver,然后它开始根据相关的LifetimeManager正确解析我的项目.最初,即使使用TransientLifetimeManager时,问题仍然会返回相同的实例.

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

所以我所做的就是改变

GlobalConfiguration.Configuration.DependencyResolver = new Microsoft.Practices.Unity.WebApi.UnityDependencyResolver(container);

GlobalConfiguration.Configuration.DependencyResolver = new Microsoft.Practices.Unity.WebApi.UnityHierarchicalDependencyResolver(container);

我所有的问题都解决了.

and all my problems were solved.

这篇关于Unity PerRequestLifetimeManager在不同请求中重用对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆