Castle.Windsor和HttpContextWrapper [英] Castle.Windsor and HttpContextWrapper

查看:202
本文介绍了Castle.Windsor和HttpContextWrapper的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

HttpContextWrapper和HttpContextBase,如这里解释,引入使HttpContext的更多mockable /测试。

我试图用小号#ARP建筑使用它,并击中了一些问题。

我的MVC控制器被设置为接受在构造一个HttpContextBase参数,在的Application_Start,HttpContextBase与Castle.Windor注册如下:

  container.Register(Component.For< HttpContextBase方式>()UsingFactoryMethod(
    ()=>新HttpContextWrapper(HttpContext.Current)));

这似乎是一个位工作确定,但后来我意识到城堡仅运行工厂方法一次,因此所有的请求得到原始HttpContextWrapper。真正需要的每个请求重新创建。对于该Castle.Windsor命令是:

  container.Register(Component.For< HttpContextBase()。
    LifeStyle.PerWebRequest.UsingFactoryMethod(
    ()=>新HttpContextWrapper(HttpContext.Current)));

...但事实证明,Castle.Windsor不允许LifeStyle.PerWebRequest内的Application_Start使用(<一个href=\"http://stackoverflow.com/questions/2670717/castle-perrequestlifestyle-not-recognize/2671569#2671569\">as这里)解释

我应该怎么做?有一个简单的办法解决这还是我应该放弃HttpContextWrapper并根据需要注入我自己的工厂,使新的?


解决方案

  

我的MVC控制器被设置为接受在构造函数中的参数HttpContextBase


您总得做点什么极其错误在这里,所以停止它已经太晚了,并造成损害的(物质,道德和人的伤亡:-))之前。你已经拥有控制器内部HttpContext的。

请不要在DI框架注册任何HttpContexts。 HttpContext的处理是ASP.NET的工作。

HttpContextWrapper and HttpContextBase, as explained here, were introduced to make HttpContext more mockable/testable.

I'm trying to use it with S#arp Architecture, and hitting some problems.

My MVC Controllers are set up to accept an HttpContextBase argument in the constructor, and during Application_Start, HttpContextBase is registered with Castle.Windor as follows:

container.Register(Component.For<HttpContextBase>().UsingFactoryMethod(
    () => new HttpContextWrapper(HttpContext.Current)));

This seemed to work OK for a bit, but then I realised Castle is only running that Factory method once, so all requests get the original HttpContextWrapper. Really it needs to be re-created for every request. The Castle.Windsor command for that would be:

container.Register(Component.For<HttpContextBase().
    LifeStyle.PerWebRequest.UsingFactoryMethod(
    () => new HttpContextWrapper(HttpContext.Current)));

... but it turns out that Castle.Windsor doesn't allow LifeStyle.PerWebRequest to be used within Application_Start (as explained here)

What should I be doing? Is there an easy way round this or should I give up on HttpContextWrapper and inject my own factory to make new ones as needed?

解决方案

My MVC Controllers are set up to accept an HttpContextBase argument in the constructor

You gotta be doing something extremely wrong here, so stop before it's too late and damage has been caused (material, moral and human casualties :-)). You already have the HttpContext inside the controller.

Don't register any HttpContexts in your DI framework. The HttpContext handling is the job of ASP.NET.

这篇关于Castle.Windsor和HttpContextWrapper的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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