开发者

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)
{
    H开发者_C百科ttpApplication 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.


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.


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

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜