Event handlers can only be associated with HttpApplication events during IHttpModule initialization.

I get the following error

'Event handlers can only bind to HttpApplication events during IHttpModule initialization.' in the following code (line in bold or double **)

protected void Application_BeginRequest(object sender, EventArgs e) { HttpApplication app = (HttpApplication)sender; **app.EndRequest += new EventHandler(Application_EndRequest);** } protected void Application_EndRequest(object sender, EventArgs e) { UnitOfWork.Commit(); } 

which is specified in the Global.asax file. Can someone figure out where I am missing? Thanks.

+6
global-asax
source share
2 answers

The event handler lives the whole life of your application, so you need to add it only after you do not add it every request. The event itself will trigger each request, and only one handler will be called each time the event is raised.

Add it to Application_Start in global.asax, not Application_BeginRequest , or better, create an HTTP module instead.

Also, I think you might not even need an event handler . A method with the current name will be called by convention, similar to the Page / Control AutoEventWireup parameter (for example, Page_Load). Note that this may have problems in ASP.NET MVC applications, as some people report. So my suggestion is to rename the function, add an event handler to Application_Start or better in the new HTTP module that you are creating .

+7
source

Try commenting on the line marked with "**". Asp.Net will call the appropriate methods on its own if you follow the naming conventions: "{Scope}" _ "{Event}", where "{Scope}" is the application if you want to handle application-level events or "session" if want to handle session-level events, and "{Event}" is the name of the event, for example, "Start," "End," etc. Additional information: http://msdn.microsoft.com/en-us/library/bb470252.aspx#Stages

+3
source

All Articles