不使用 HttpModule 处理声明性每个请求数据
我有一个“上下文”对象,它通过静态 Current 属性将自身绑定到 HttpContext.Items 。 该对象可以通过表达式生成器和控件直接使用,所有这些都是同一库的一部分。
我遇到的问题是,当请求结束而不使用 HttpModule 时,我想处理它的托管资源(WCF 客户端)(因为否则不需要)。 我已经实现了完全一次性模式(使用Finaliser),但您不能依赖 GC 及时完成实例。
我尝试在对象构造函数中订阅 HttpApplication.EndRequest (也是 PostRequestHandlerExecute)的静态方法(通过锁定以确保它只发生一次),但该事件从未被调度。 这可能是因为 HttpApplication 的实例发生了变化,尽管我不完全确定。
关于解决这个问题的最佳方法有什么想法吗?
I have a 'context' object that ties itself to HttpContext.Items via a static Current property. This object can be used directly, through expression builders and controls, all being part of the same library.
The issue I'm coming across is that I want to dispose of it's managed resources (WCF clients) when a request ends without using an HttpModule (since it is otherwise not needed). I have implemented the full disposable pattern (with Finaliser), but you can't rely on the GC finalising instances in a timely manner.
I have tried to subscribe a static method to HttpApplication.EndRequest (also PostRequestHandlerExecute) in the objects constructor (with locking to ensure it only happens once), but the event is never dispatched. This is presumably because the instance of HttpApplication changes, though I'm not entirely sure.
Any thoughts on the best way to go about solving this?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
订阅静态事件的请求将导致内存泄漏。 它可以防止请求被收集,因为它包含对 HttpApplication 的引用。
如果您想对缓存在
HttpContext.Items
中的项目调用 dispose,您需要在请求结束时执行此操作。 如果您不想使用 HttpModule,请使用 Global.ascx 并挂钩EndRequest
事件。Subscribing requests to a static event will cause a memory leak. It prevents the request from beging garage collected because it holds a reference to HttpApplication.
If you want to call dispose on an item cached in
HttpContext.Items
you need to do this at the end of the request. If you don't want to use a HttpModule use Global.ascx and hook theEndRequest
event.