asp.net-mvc ASP.NET MVC 不调用 global.asax' EndRequest
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/764298/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
ASP.NET MVC doesn't call global.asax' EndRequest
提问by user87338
I am trying to perform some actions at the end of every request. I changed the Application_Start() that is generated when created new project to make a test:
我试图在每个请求结束时执行一些操作。我更改了创建新项目时生成的 Application_Start() 进行测试:
protected void Application_Start()
{
EndRequest += (s, e) =>
{
Console.Write("fghfgh");
};
RegisterRoutes(RouteTable.Routes);
}
The lambda won't get called. Any ideas why?
lambda 不会被调用。任何想法为什么?
edit: I see that they are doing similar thing in SharpArch [http://code.google.com/p/sharp-architecture/]and it does work there... And no, I don't want to use an HttpModule.
编辑:我看到他们在 SharpArch [ http://code.google.com/p/sharp-architecture/]中做类似的事情并且它确实在那里工作......不,我不想使用 HttpModule .
edit2: The only workaround I found is to use Application_EndRequest in conjuction with a private static member of global.asax:
edit2:我发现的唯一解决方法是将 Application_EndRequest 与 global.asax 的私有静态成员结合使用:
private static WebSessionStorage wss;
protected void Application_Start()
{
//...
wss = new WebSessionStorage(this);
//...
}
protected void Application_EndRequest(object sender, EventArgs e)
{
wss.EndRequest(sender, e);
}
wss must be private because it seems like the Application_EndRequest is being called using different instance object (this). That may also be reason of my event (as described at the beginning) not being called.
wss 必须是私有的,因为似乎 Application_EndRequest 正在使用不同的实例对象(this)被调用。这也可能是我的事件(如开头所述)未被调用的原因。
回答by Jabe
I usually do:
我通常这样做:
protected void Application_EndRequest(object sender, EventArgs e)
{
}
This works as expected. Don't know about the event though.
这按预期工作。虽然不知道事件。
回答by Eilon
The HttpApplication instance that is represented by your global.asax file is a single instance that only represents the firstHttpApplication object. It is not guaranteed that this instance of the HttpApplication will be used for any other request.
由 global.asax 文件表示的HttpApplication实例是仅表示第一个HttpApplication 对象的单个实例。不保证此 HttpApplication 实例将用于任何其他请求。
You need to override the Init() method in global.asax and in thatmethod hook up any events that you want:
您需要覆盖 global.asax 中的 Init() 方法,并在该方法中连接您想要的任何事件:
public override void Init() {
base.Init();
EndRequest += MyEventHandler;
}
Please refer to this MSDN articlefor more info on the HttpApplication object.
有关 HttpApplication 对象的更多信息,请参阅此 MSDN 文章。
回答by James Avery
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.
最好的办法是在 HttpModule 中执行此操作。我使用 HttpModule 在 MVC 应用程序中管理 NHibernate 会话,它运行良好。在开始请求中,我将 sessionFactory 绑定到 ManagedWebSessionContext(在 NHibernate 中但相当未记录),然后在结束请求中我提交任何事务并解除 sessionFactory 的绑定。
I think it is cleaner to separate this into an HttpModule as well.
我认为将它分成一个 HttpModule 也更干净。

