问题描述
我的问题与以下类似但不完全相同:
My question is similar but not identical to:
为什么我的主机(softsyshosting.com)是否支持 BeginRequest 和 EndRequest 事件处理程序?(我还阅读了其中引用的 mvolo 博客)
Why can't my host (softsyshosting.com) support BeginRequest and EndRequest event handlers? (I've also read the mvolo blog referenced therein)
我们的目标是使用通过 system.webServer 配置集成的普通 HttpModule 成功地将 HttpApplication.BeginRequest 挂接到 IHttpModule.Init 事件(或模块内部的任何位置)中,即没有:
The goal is to successfully hook HttpApplication.BeginRequest in the IHttpModule.Init event (or anywhere internal to the module), using a normal HttpModule integrated via the system.webServer config, i.e. one that doesn't:
- 入侵 Global.asax 或
覆盖 HttpApplication(该模块旨在自包含且可重复使用,例如,我有这样的配置):
- invade Global.asax or
override the HttpApplication (the module is intended to be self-contained & reusable, so e.g. I have a config like this):
<system.webServer>
<validation validateIntegratedModeConfiguration="false"/>
<modules>
<remove name="TheHttpModule" />
<add name="TheHttpModule" type="Company.HttpModules.TheHttpModule" preCondition="managedHandler" />
到目前为止,我尝试将侦听器附加到 HttpApplication.BeginRequest 的任何策略都会导致以下两种情况之一,症状 1 是 BeginRequest 永远不会触发,或者症状 2 是在所有托管请求上引发以下异常,我无法捕捉 &从用户代码处理它:
So far, any strategy I've tried to attach a listener to HttpApplication.BeginRequest results in one of two things, symptom 1 is that BeginRequest never fires, or symptom 2 is that the following exception gets thrown on all managed requests, and I cannot catch & handle it from user code:
Stack Trace:
[NullReferenceException: Object reference not set to an instance of an object.]
System.Web.PipelineModuleStepContainer.GetEventCount(RequestNotification notification, Boolean isPostEvent) +30
System.Web.PipelineStepManager.ResumeSteps(Exception error) +1112
System.Web.HttpApplication.BeginProcessRequestNotification(HttpContext context, AsyncCallback cb) +113
System.Web.HttpRuntime.ProcessRequestNotificationPrivate(IIS7WorkerRequest wr, HttpContext context) +616
在 Init 中注释掉 app.BeginRequest += new EventHandler(this.OnBeginRequest)
当然会停止异常.Init 根本不引用 Context 或 Request 对象.
Commenting out app.BeginRequest += new EventHandler(this.OnBeginRequest)
in Init stops the exception of course. Init does not reference the Context or Request objects at all.
我试过了:
- 删除了所有项目中任何地方对 HttpContext.Current 的引用(仍然是症状 1)
- 测试从我的 OnBeginRequest 方法的主体中删除所有代码,以确保问题不是方法内部的(= 异常)
- 嗅探堆栈跟踪并仅在 InitializeApplication 未启动堆栈时调用 app.BeginRequest+=...(= BeginRequest 未触发)
- 仅在第二次通过 Init 时调用 app.BeginRequest+=(= BeginRequest 未触发)
- Removed all references to HttpContext.Current anywhere in the project (still symptom 1)
- Tested removing all code from the body of my OnBeginRequest method, to ensure the problem wasn't internal to the method (= exception)
- Sniffing the stack trace and only calling app.BeginRequest+=... when if the stack isn't started by InitializeApplication (= BeginRequest not firing)
- Only calling app.BeginRequest+= on the second pass through Init (= BeginRequest not firing)
有人知道一个好的方法吗?是否有一些在模块中挂钩 Application_Start 的间接策略(似乎不太可能)?另一个事件,a) 可以从模块的构造函数或 Init 方法中挂钩,并且 b) 随后是附加 BeginRequest 事件处理程序的安全位置?
Anyone know of a good approach? Is there some indirect strategy for hooking Application_Start within the module (seems unlikely)? Another event which a) one can hook from a module's constructor or Init method, and b) which is subsequently a safe place to attach BeginRequest event handlers?
非常感谢
推荐答案
您的 HttpModule 的 Init 方法将被单个 Web 应用程序多次调用(而您 global.asax 中的 Application_Start 只会被每个 AppDomain 调用一次).
Your HttpModule's Init method will get called multiple times by a single web application (whereas Application_Start in your global.asax will only get called once per AppDomain).
Init 确实是连接 BeginRequest 的地方.
Init is indeed the place to hook onto BeginRequest.
我也遇到过这个错误,它是由多次挂接到 BeginRequest 事件引起的.我不确定这是否是 IIS 7 集成模式中的错误...
I have encountered this error as well and it was caused by hooking onto the BeginRequest event more than once. I'm not sure if it is a bug in IIS 7 integrated mode or not...
当您执行 app.BeginRequest 时,您是使用 IHttpModule 的 Init 方法的 context 参数调用 context.BeginRequest 还是调用 HttpContext.Current.BeginRequest += ...?
When you do app.BeginRequest are you calling context.BeginRequest using the context parameter to your IHttpModule's Init method or are you calling HttpContext.Current.BeginRequest += ...?
这篇关于HttpModule.Init - 在 IIS7 集成模式下安全地添加 HttpApplication.BeginRequest 处理程序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!