ASP.NET MVC和放大器; Windsor.Castle:与HttpContext的相关服务工作 [英] ASP.NET MVC & Windsor.Castle: working with HttpContext-dependent services

查看:115
本文介绍了ASP.NET MVC和放大器; Windsor.Castle:与HttpContext的相关服务工作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有几个依赖注入服务,这都依赖于像HTTP上下文的东西。现在,我在配置它们作为单身温莎容器在Application_Start处理程序中,这显然是对这类服务的问题。

I have several dependency injection services which are dependent on stuff like HTTP context. Right now I'm configuring them as singletons the Windsor container in the Application_Start handler, which is obviously a problem for such services.

什么是处理这个问题的最好方法?我正在考虑让他们的瞬态,然后每个HTTP请求后释放他们。但是,什么是最好的方法/地方注入HTTP上下文放进去?控制器工厂或别的地方?

What is the best way to handle this? I'm considering making them transient and then releasing them after each HTTP request. But what is the best way/place to inject the HTTP context into them? Controller factory or somewhere else?

推荐答案

通过温莎城堡,你可以使用 PerWebRequest 一生 - 这应该适合pretty以及与要求。

With Castle Windsor you can use the PerWebRequest lifetime - that should fit pretty well with your requirements.

这意味着你可以注入HTTP的东西进入你的服务,并且容器将采取适当的生命周期管理的照顾。然而,这需要你同时注册所有这些服务(和所有消费者的服务等)为PerWebRequest(或瞬态),因为如果你把它们注册为单身,他们会坚持到陈旧的(也可能是处置)上下文。

That means you can just inject the HTTP stuff into your services, and the container will take care of the proper lifetime management. However, this requires you to also register all these services (and all consumers of those services and so on) as PerWebRequest (or Transient) because if you register them as Singletons, they will hold on to stale (and possibly disposed) contexts.

这篇关于ASP.NET MVC和放大器; Windsor.Castle:与HttpContext的相关服务工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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