NHibernate.HibernateException:无会话绑定到当前上下文
-
16-09-2019 - |
题
我收到的时候我试图让CurrentSession这个错误
NHibernate.Context.CurrentSessionContext.CurrentSession()
在
NHibernate.Impl.SessionFactoryImpl.GetCurrentSession()
解决方案
您是负责对会话上下文设置当前会话。请参见 NHibernate的文档的这一部分。如果你还没有这样做,那么就不会有当前会话检索。
其他提示
像大卫中号说,你需要确保你有约束力的NHibernate的会议。下面是我做的,现在在我的ASP.NET应用程序的方式:
public class NHHttpModule : IHttpModule
{
public void Init(HttpApplication context)
{
context.EndRequest += ApplicationEndRequest;
context.BeginRequest += ApplicationBeginRequest;
}
public void ApplicationBeginRequest(object sender, EventArgs e)
{
CurrentSessionContext.Bind(NHSessionFactory.GetNewSession());
}
public void ApplicationEndRequest(object sender, EventArgs e)
{
ISession currentSession = CurrentSessionContext.Unbind(
NHSessionFactory.GetSessionFactory());
currentSession.Close();
currentSession.Dispose();
}
public void Dispose()
{
// Do nothing
}
}
创建结合在每次请求我的会话的自定义HTTP模块,然后我这个模块添加到我的web.config这样的:
<httpModules>
<add name="NHHttpModule" type="MyApplication.Core.NHHttpModule, MyApplication,
Version=1.0.0.0, Culture=neutral" />
</httpModules>
我敢肯定你的配置不同,则但这只是我如何结合我的会话的一个示例。希望这有助于一点。
演播室2010将创建2个的HttpModules部分,一个是IIS 7,一定要在对System.Web一个太注册您的NHibernate的HttpModule。
不隶属于 StackOverflow