如何使用Unity 2.0和asp.net mvc的每注入HTTP请求(或每HTTP上下文)的依赖 [英] How to inject dependencies per http request (or per http context) with unity 2.0 and asp.net mvc

查看:84
本文介绍了如何使用Unity 2.0和asp.net mvc的每注入HTTP请求(或每HTTP上下文)的依赖的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经读了很多关于这个信息,但我找不到任何电流(为1/2011)作为究竟如何这应该是今天做的。我读过一对夫妇,似乎有了答案好职位,没有细节。年长pre-单位2的方法主要是创建的自定义生命周期管理。看来最好的办法做到这一点现在是使用一个子容器在请求的开始被创建并设置在请求结束,使用容器管理的生命周期。另一个人说,他们已经做到了这一点使用层次一生与createchildcontainer

I've read a lot of info on this, but I can't find anything current (as of 1/2011) as to how exactly this should be done nowadays. I've read a couple of good posts that seem to have the answer, without the details. Older pre-unity-2 approaches mostly created custom lifetime managers. It appears the best way to do it now is to use a child container that is created at the beginning of the request and disposed at the end of the request, using container-managed lifetime. Another person said they've achieved this using hierarchical lifetime with createchildcontainer.

大多数我找到了材料说的Application_BeginRequest,我已经试过在创建子容器。这并不工作,因为我的控制器厂(IoCControllerFactory从MvcContrib)使用的Application_Start(在的IDependencyResolver的MvcContrib实现),这是默认/根容器中注册的容器中。

Most of the material I found says to create the child container during Application_BeginRequest, which I've tried. That doesn't work because my controller factory (IoCControllerFactory from MvcContrib) uses the container registered during Application_Start (an implementation of IDependencyResolver in MvcContrib), which is the default/root container.

我在我的web.config中设置的团结,我可以创建子容器,但我似乎无法得到任何的控制器使用它们DI创造每个请求单身。我需要的,而不是根目录中创建一个特殊的容器中的控制器工厂使用,?它应该是容器管理或层级?我不应该再设置DependencyResolver每个请求,我应该?

I have my unity setup in web.config, and I can create child containers, but I can't seem to get any controllers to use them for DI to create singletons per request. Do I need to create a special container to use in the controller factory, instead of the root? Should it be container-managed or hierarchical? I shouldn't be re-setting the DependencyResolver on every request, should I?

推荐答案

尝试 HTTP://mvcunity.codeplex.com / 或下载源$ C ​​$ C,看看它是如何做。

Try http://mvcunity.codeplex.com/ or download the source code to see how it's done.

这篇关于如何使用Unity 2.0和asp.net mvc的每注入HTTP请求(或每HTTP上下文)的依赖的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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