我有NHibernate掛在我的asp.net mvc應用程序。如何在ASP.NET MVC應用程序中部署NHibernate ISession
一切工作正常,如果我不處置ISession。不過,我已經讀過,你應該處置,但是當我這樣做時,我會隨機獲得「Session is closed」異常。
我正在將ISession注入到Windsor的其他對象中。
這是我目前的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
}
}
中註冊的Isession:
container
.Register(Component.For<ISession>()
.UsingFactoryMethod(() => MvcApplication.SessionFactory.GetCurrentSession()).LifeStyle.Transient);
當我釘的Dispose在模塊中取消綁定錯誤發生。由於我不斷收到會話是關閉錯誤,我認爲這不是正確的方式來做到這一點,那麼正確的方法是什麼?
謝謝, 喬