问题描述
我的问题类似但不完全相同:
My question is similar but not identical to:
(我还阅读了其中引用的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在IHttpModule.Init事件中(或模块内部的任何地方)成功挂钩HttpApplication.BeginRequest。配置,即不配:
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方法的主体中删除所有代码,确保问题不在方法内部(=异常)
- 嗅探堆栈跟踪并仅调用app.BeginRequest + = ...如果堆栈未由InitializeApplication启动( = BeginRequest未触发)
- 仅在第二次通过Init(= BeginRequest未触发)时调用app.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时,您是否使用上下文调用context.BeginRequest您的IHttpModule的Init方法的参数或您是否正在调用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处理程序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!