2012-10-02 52 views
1

我有一個流利的NHibernate設置在ASP.NET Web應用程序。我有一個攔截請求,併爲每一個新的會話HTTP模塊:流利的NHibernate會話不能在ASP.NET Web應用程序工作

private static void BeginRequest(object sender, EventArgs e) 
{ 

    ISession session = _sessionFactory.OpenSession(); 

    session.BeginTransaction(); 

    CurrentSessionContext.Bind(session); 
} 

它配置,像這樣:

private static ISessionFactory CreateSessionFactory() 
{ 
    return Fluently 
     .Configure() 
     .Database(MsSqlConfiguration.MsSql2005 
      .ConnectionString(c => c 
       .FromConnectionStringWithKey("RecruitmentApp"))) 
     .Mappings( 
      m => m.FluentMappings.AddFromAssemblyOf<RecruitmentAppLibrary.Applicant>() 
     ) 
     .ExposeConfiguration(c => c.SetProperty(NHibernate.Cfg.Environment.CurrentSessionContextClass, "web")) 
     .BuildSessionFactory(); 
} 

我設置當前會話的上下文類「網絡」但是,當調用_sessionFactory.GetCurrentSession()時,代碼無法獲得會話。它說「沒有會話綁定到當前上下文」。我對它進行了一些調試,會話被插入到Http上下文中,但由於某種原因(即使在調用Page_Load時它仍然在上下文中),它無法將其拉回。有任何想法嗎?

+0

當您解除綁定會話環境?在你使用它的地方放置一個斷點,並確保在你嘗試使用它之前不要解除綁定。 –

回答

0

嘗試使用,而不是CurrentSessionContext ManagedWebSessionContext:

ManagedWebSessionContext.Bind(HttpContext.Current, session); 
相關問題