“会话已结束!” -NHibernate
这是在 Web 应用程序环境中:
初始请求能够成功完成,但是任何其他请求都会从 NHibernate 框架返回“会话已关闭”响应。我使用带有以下代码的 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<MyObject>(id);
}
}
这允许应用程序中的代码这样调用:
IMyObjectRepository repo = new MyObjectRepository();
MyObject obj = repo.GetByID(1);
我怀疑我的存储库代码是罪魁祸首,但我不能 100% 确定我应该执行的实际实现正在使用。
我在此处上发现了类似的问题。我也在我的实现中使用 WebSessionContext,但是,除了编写自定义 SessionManager 之外,没有提供任何解决方案。对于简单的 CRUD 操作,除了内置工具(即 WebSessionContext)之外,是否还需要自定义会话提供程序?
This is in a web application environment:
An initial request is able to successfully complete, however any additional requests return a "Session is Closed" response from the NHibernate framework. I'm using a HttpModule approach with the following code:
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 is my singleton implementation, using FluentNHibernate to return an ISessionFactory object.
In my repository class, I attempt to use the following syntax:
public class MyObjectRepository : IMyObjectRepository
{
public MyObject GetByID(int id)
{
using (ISession session = SessionFactory.Instance.GetCurrentSession())
return session.Get<MyObject>(id);
}
}
This allows code in the application to be called as such:
IMyObjectRepository repo = new MyObjectRepository();
MyObject obj = repo.GetByID(1);
I have a suspicion my repository code is to blame, but I'm not 100% sure on the actual implementation I should be using.
I found a similar issue on SO here. I too am using WebSessionContext in my implementation, however, no solution was provided other than writing a custom SessionManager. For simple CRUD operations, is a custom session provider required apart from the built in tools(ie WebSessionContext)?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
我还没有测试你的代码,但从阅读来看,这一行:
在块退出后转储你的会话,然后会话在下次通过时被处置/无效。
这是我们在应用程序中使用的模型:
I haven't tested your code, but from reading, this line:
is dumping your session after the block exits, and then the session is disposed/invalid on the next time through.
Here's the model we're using in our applications:
我遇到了类似的错误。事实证明,我是在“新建”我的存储库,而不是让我的 IOC 容器解决它。
I got a similar error. Turns out I was "new"ing up my repository instead of having my IOC container resolve it.
使用以下语句会在每次查询后处理或关闭会话:
而是在不使用“using”一词的情况下使用它:
这对我有用。
Use of the following statement disposes off or closes the session after every query:
Instead use it without the "using" word as:
This worked for me.