I have NHibernate connected in my asp.net mvc application.
Everything works fine if I DO NOT DELETE. However, I read that you should dispose, but when I do this, I get random exceptions "session closed".
I injected ISession into my other objects using 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()
{
}
}
Register ISession:
container
.Register(Component.For<ISession>()
.UsingFactoryMethod(() => MvcApplication.SessionFactory.GetCurrentSession()).LifeStyle.Transient);
The error occurs when I bind Dispose to unbind in the module. As I continue to consider the session a closed error, I assume that this is not the right way to do this, so is this the right way?
Thanks Joe
source
share