My question is similar but not identical to:
Why can't my host (softsyshosting.com) support BeginRequest and EndRequest event handlers? (I\'ve also read the mvol
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 is indeed the place to hook onto BeginRequest.
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...
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 += ...?
I had the same problems described above. I found that a practical work around was to always remove then add the handler. thus:
public override void Init()
{
base.Init();
lock (_initialisationLockObject)
{
BeginRequest -= Global_BeginRequest;
BeginRequest += Global_BeginRequest;
}
}
I suspect that the event handlers are getting cleared down after one or more of the multiple calls to the init. If you carefully only try and add the event handler the first time, the later times init gets called don't get a event added and hence the handler doesn't get called at all. If you don't try anything cunning to limit the number of times the event gets added, then you seen to get multiple attachments. By first trying to remove then add (inside a lock to stop any gnarly race conditions) seems to do the trick.
It is horrible though, and we shouldn't have to do this!
Hope that helps