Application_EndRequest Dosent Fire on a 404

Posted by Shane on Stack Overflow See other posts from Stack Overflow or by Shane
Published on 2010-03-15T21:39:25Z Indexed on 2010/03/15 21:49 UTC
Read the original article Hit count: 182

Filed under:
|
|

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

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

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.

© Stack Overflow or respective owner

Related posts about nhibernate

Related posts about session