Castle.Windsor 和 HttpContextWrapper

发布于 2024-10-27 03:18:49 字数 1190 浏览 8 评论 0 原文

引入 HttpContextWrapper 和 HttpContextBase,正如这里解释的,是为了让 HttpContext 更加可模拟/可测试。

我尝试将它与 S#arp Architecture 一起使用,并遇到了一些问题。

我的 MVC 控制器设置为在构造函数中接受 HttpContextBase 参数,并且在 Application_Start 期间,HttpContextBase 向 Castle.Windor 注册,如下所示:

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

这似乎工作正常一段时间,但后来我意识到 Castle 只运行该工厂方法一次,因此所有请求都会得到原始的 HttpContextWrapper。实际上,它需要为每个请求重新创建。 Castle.Windsor 命令是:

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

...但事实证明 Castle.Windsor 不允许在 Application_Start 中使用 LifeStyle.PerWebRequest (如此处所述

我应该做什么?有没有一种简单的方法可以解决这个问题,或者我应该放弃 HttpContextWrapper 并注入我自己的工厂来根据需要创建新的工厂?

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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

牵你的手,一向走下去 2024-11-03 03:18:49

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

您必须在这里做一些极其错误的事情,所以在为时已晚并造成损害之前停止(物质、道德和人员伤亡:- ))。您已经在控制器内拥有 HttpContext。

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

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.

累赘 2024-11-03 03:18:49

正如 Darin 指出的,将 HttpContext 注入 MVC 控制器是没有意义的。但是,如果您需要它用于其他类型的服务并且在 Application_Start() 中也需要它,请使用 混合的 perwebrequest-transient 生活方式。或者,由于构建起来很简单,因此只需使其短暂即可。

As Darin noted, it makes no sense to inject an HttpContext into an MVC controller. However, if you need it for other kind of services and also need it in Application_Start(), use an hybrid perwebrequest-transient lifestyle. Or, since it's trivial to build, just make it transient.

左岸枫 2024-11-03 03:18:49

正如其他人所说 - 你做错了。我的大问题是:

你在做什么,需要你在控制器中注入 HttpContextBase ?如果您能为我们提供更多有关您真正想做的事情的背景信息,这可能会对想要帮助您的人更有帮助。让我们把 Castle 拿出来,看看你的控制器在做什么。

顺便说一句,您的控制器已经有对 HttpContext 的引用。如果您这样做是为了可测试性,则无需在控制器级别执行任何操作。您只需要根据控制器测试中的需要模拟 HttpContextBase 对象。

As others have stated - you are doing it wrong. My big question is:

What are you doing that requires you to inject HttpContextBase in your controller? It might be more helpful to people wanting to help you if you would provide us more context about what you are really trying to do. Lets take Castle out of it and get down to what your controller is doing.

BTW, your controller already has a reference to HttpContext. If you are doing this for testability, there is nothing you need to do at the controller level. You would just need to mock the HttpContextBase object as needed in your controller tests.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文