Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Event handlers can only be bound to HttpApplication events during IHttpModule initialization

I am getting the following error

'Event handlers can only be bound to HttpApplication events during IHttpModule initialization.' at 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 mentioned in Global.asax file. Can anybody figure out, where I am lacking? Thanks.

like image 998
Zohaib Avatar asked Mar 03 '11 05:03

Zohaib


People also ask

What are the event handlers in global ASAX?

Application_Error() – fired when an error occurs. Session_End() – fired when the session of a user ends. Application_End() – fired when the web application ends. Application_Disposed() - fired when the web application is destroyed.

What is HttpApplication?

The HttpApplication class is used to access application wide information within an ASP.NET web application or site.

What is the difference between event and EventHandler?

The event is the thing that RAISES an event, to which something will subscribe. The EventHandler is the thing that HANDLES an event - i.e. it specifies the method that is used to subscribe to the event.

What is HttpApplication class?

Defines the methods, properties, and events that are common to all application objects in an ASP.NET application. This class is the base class for applications that are defined by the user in the Global.


2 Answers

The event handler lives the entire life of your application, so, you only need to add it once not add it every request. The event itself will fire every request, and the only-one handler will be called every 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 may not even need an event handler. The method with current name will be called by convention similar to Page/Control AutoEventWireup (like Page_Load). Just note that this might have issues in ASP.NET MVC applications as some people report. So, my suggestion is to rename the function, add the event handler in Application_Start, or better in a new HTTP Module you create.

like image 131
Meligy Avatar answered Nov 15 '22 08:11

Meligy


Try to comment out line marked with "**". Asp.Net will call appropriate methods by itself if followed by naming conventions: "{Scope}"_"{Event}", where "{Scope}" is Application if you want to handle application level events or "Session" if you want to handle session level events, and "{Event}" is name of the event, like Start, End, etc. More info: http://msdn.microsoft.com/en-us/library/bb470252.aspx#Stages

like image 26
wałdis iljuczonok Avatar answered Nov 15 '22 07:11

wałdis iljuczonok