【发布时间】:2011-04-12 08:57:38
【问题描述】:
我的问题与以下类似但不完全相同:
Why can't my host (softsyshosting.com) support BeginRequest and EndRequest event handlers?(我还阅读了其中引用的 mvolo 博客)
我们的目标是使用通过 system.webServer 配置集成的普通 HttpModule 成功地将 HttpApplication.BeginRequest 挂接到 IHttpModule.Init 事件(或模块内部的任何位置)中,即没有:
- 入侵 Global.asax 或
-
覆盖 HttpApplication(该模块旨在自包含和可重用,例如,我有这样的配置):
<system.webServer> <validation validateIntegratedModeConfiguration="false"/> <modules> <remove name="TheHttpModule" /> <add name="TheHttpModule" type="Company.HttpModules.TheHttpModule" preCondition="managedHandler" />
到目前为止,我尝试将侦听器附加到 HttpApplication.BeginRequest 的任何策略都会导致以下两种情况之一,症状 1 是 BeginRequest 永远不会触发,或者症状 2 是在所有托管请求上引发以下异常,我无法从用户代码中捕获和处理它:
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 对象。
我试过了:
- 删除了所有对项目中任何位置的 HttpContext.Current 的引用(仍然是症状 1)
- 已测试从我的 OnBeginRequest 方法主体中删除所有代码,以确保问题不是方法内部的问题(= 异常)
- 嗅探堆栈跟踪并仅在 InitializeApplication 未启动堆栈时调用 app.BeginRequest+=...(= BeginRequest 未触发)
- 仅在第二次通过 Init 时调用 app.BeginRequest+=(= BeginRequest 未触发)
有人知道一个好的方法吗?是否有一些在模块中挂钩 Application_Start 的间接策略(似乎不太可能)?另一个事件,a) 可以从模块的构造函数或 Init 方法中挂钩,并且 b) 随后是附加 BeginRequest 事件处理程序的安全位置?
非常感谢
【问题讨论】:
标签: c# iis-7 httpmodule integrated-pipeline-mode