views:

170

answers:

3

I have NHibernate hooked up in my asp.net mvc app.

Everything works fine, if I DON'T dispose the ISession. I have read however that you should dispose, but when I do, I get random "Session is closed" exceptions.

I am injecting the ISession into my other objects with Windsor.

Here is my current NHModule:

public class NHibernateHttpModule : IHttpModule
{
    public void Init(HttpApplication context)
    {
        context.BeginRequest += context_BeginRequest;
        context.EndRequest += context_EndRequest;
    }

    static void context_EndRequest(object sender, EventArgs e)
    {
        CurrentSessionContext.Unbind(MvcApplication.SessionFactory);
    }

    static void context_BeginRequest(object sender, EventArgs e)
    {
        CurrentSessionContext.Bind(MvcApplication.SessionFactory.OpenSession());
    }

    public void Dispose()
    {
        // do nothing
    }
}

Registering the ISession:

container
  .Register(Component.For<ISession>()
     .UsingFactoryMethod(() =>  MvcApplication.SessionFactory.GetCurrentSession()).LifeStyle.Transient);

The error happens when I tack the Dispose on the unbind in the module. Since I keep getting the session is closed error I assume this is not the correct way to do this, so what is the correct way?

Thanks, Joe

A: 

It seems some object holding the session gets disposed, before Windsor tries to dispose the session. A quick fix is to ovveride your NHiberanteSessionFactory Dispose method and check if the session is already closed.

Robert
A: 

I think this article should help: Building a Desktop To-Do Application with NHibernate

Now I know, this is a Desktop application, but you might get inspired from this MSDN article.

Will Marcouiller
+1  A: 

Robert is correct, Windsor is disposing the session when it is releasing the components that depend on it. Rather than having your own HttpModule I'd recommend using Windsor's PerWebRequest lifestyle for ISession.

mattk