ASP.NET MVC doesn't call global.asax' EndRequest

假装没事ソ 提交于 2019-11-27 22:14:01
Jabe

I usually do:

protected void Application_EndRequest(object sender, EventArgs e)
{
}

This works as expected. Don't know about the event though.

The HttpApplication instance that is represented by your global.asax file is a single instance that only represents the first HttpApplication object. It is not guaranteed that this instance of the HttpApplication will be used for any other request.

You need to override the Init() method in global.asax and in that method hook up any events that you want:

public override void Init() {
    base.Init();

    EndRequest += MyEventHandler;
}

Please refer to this MSDN article for more info on the HttpApplication object.

Your best bet is to do this in an HttpModule. I use an HttpModule to manage NHibernate session in an MVC app and it works perfectly. In the begin request I bind the sessionFactory to the ManagedWebSessionContext (in NHibernate but fairly undocumented) and then in the end request I commit any transactions and unbind the sessionFactory.

I think it is cleaner to separate this into an HttpModule as well.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!