Application_EndRequest不会在404上触发 [英] Application_EndRequest Doesn't Fire on a 404

查看:105
本文介绍了Application_EndRequest不会在404上触发的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用ASP MVC 2和Nhibernate.我已经创建了一个HTTP模块,如NHibernate 13的夏季演示中所示:

I am using ASP MVC 2 and Nhibernate. I have created an HTTP Module as demonstrated in Summer of NHibernate 13 that looks like so:

public void Init(HttpApplication context)
{
     context.PreRequestHandlerExecute += new EventHandler(Application_BeginRequest);
     context.PostRequestHandlerExecute += new EventHandler(Application_EndRequest);
}

private void Application_BeginRequest(object sender, EventArgs e)
{

    ISession session = StaticSessionManager.OpenSession();
    session.BeginTransaction();
    CurrentSessionContext.Bind(session);
}

private void Application_EndRequest(object sender, EventArgs e)
{
  ISession session = CurrentSessionContext.Unbind(StaticSessionManager.SessionFactory);
  if (session != null)
  try
  {
      session.Transaction.Commit();
   }
   catch (Exception)
   {
       session.Transaction.Rollback();
   }
   finally
   {
       session.Flush();
       session.Close();
    }
}

web.config

web.config

<add name="UnitOfWork" type="HttpModules.UnitOfWork"/>

我的问题是Application_EndRequest永远不会在404错误上被调用,因此,如果我的视图未呈现,我将完全阻止数据库访问,直到发生刷新.我是NHibernate的新手,所以不确定是否丢失了一些东西.

My problem is that Application_EndRequest never gets called on a 404 error so if my view does not render I completely block database access until my flush takes place. I am fairly new to NHibernate so I am not sure if I am missing something.

推荐答案

您可以在application_error中处置并回滚会话.将session.close更改为session.dispose并检查它是否尚未被处理.

You can dispose and rollback the session in application_error. change session.close into session.dispose and check if it's not already disposed.

这篇关于Application_EndRequest不会在404上触发的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆