2010-01-24 85 views
3

我有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在模塊中取消綁定錯誤發生。由於我不斷收到會話是關閉錯誤,我認爲這不是正確的方式來做到這一點,那麼正確的方法是什麼?

謝謝, 喬

回答

1

羅伯特是正確的,溫莎正在釋放會議,當它釋放依賴它的組件。 而不是有自己的HttpModule,我建議使用溫莎的PerWebRequest生活方式爲ISession。

0

似乎有些對象持有的會議得到處理,溫莎試圖處置會話之前。一個快速解決方法是將你的NHiberanteSessionFactory Dispose方法包裝起來,並檢查會話是否已經關閉。