“会议已结束!” – NHibernate

这是在Web应用程序环境中:

初始请求能够成功完成,但是任何其他请求都会从NHibernate框架返回“Session is Closed”响应。 我正在使用HttpModule方法,代码如下:

public class MyHttpModule : IHttpModule { public void Init(HttpApplication context) { context.EndRequest += ApplicationEndRequest; context.BeginRequest += ApplicationBeginRequest; } public void ApplicationBeginRequest(object sender, EventArgs e) { CurrentSessionContext.Bind(SessionFactory.Instance.OpenSession()); } public void ApplicationEndRequest(object sender, EventArgs e) { ISession currentSession = CurrentSessionContext.Unbind( SessionFactory.Instance); currentSession.Dispose(); } public void Dispose() { } } 

SessionFactory.Instance是我的单例实现,使用FluentNHibernate返回一个ISessionFactory对象。

在我的存储库类中,我尝试使用以下语法:

 public class MyObjectRepository : IMyObjectRepository { public MyObject GetByID(int id) { using (ISession session = SessionFactory.Instance.GetCurrentSession()) return session.Get(id); } } 

这允许应用程序中的代码被调用:

 IMyObjectRepository repo = new MyObjectRepository(); MyObject obj = repo.GetByID(1); 

我怀疑我的存储库代码应该受到指责,但我不能100%确定我应该使用的实际实现。

我在这里发现了类似的问题。 我也在我的实现中使用WebSessionContext,但是,除了编写自定义SessionManager之外,没有提供任何解决方案。 对于简单的CRUD操作,是否需要除内置工具(即WebSessionContext)之外的自定义会话提供程序?

我没有测试过您的代码,但是从阅读开始,这一行:

 using (ISession session = SessionFactory.Instance.GetCurrentSession()) 

在块退出后转储会话,然后在下次通过时处理/无效会话。

这是我们在应用程序中使用的模型:

 ISession session = null; try { // Creates a new session, or reconnects a disconnected session session = AcquireCurrentSession(); // Database operations go here } catch { session.Close(); throw; } finally { session.Disconnect(); } 

我得到了类似的错误。 事实certificate我是“新”我的存储库,而不是让我的IOC容器解决它。

使用以下语句在每次查询后处理关闭或关闭会话:

 using (ISession session = SessionFactory.Instance.GetCurrentSession()) 

而是在没有“使用”字的情况下使用它:

 ISession session = SessionFactory.Instance.GetCurrentSession() 

这对我有用。