没有会话绑定到当前上下文 [英] No session bound to the current context

查看:193
本文介绍了没有会话绑定到当前上下文的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我跟着这个教程:<一href=\"http://nhforge.org/blogs/nhibernate/archive/2011/03/03/effective-nhibernate-session-management-for-web-apps.aspx\" rel=\"nofollow\">http://nhforge.org/blogs/nhibernate/archive/2011/03/03/effective-nhibernate-session-management-for-web-apps.aspx

我没有得到一个尝试加载页面时错误(MVC 3)。

I am not getting a 'no session bound to the current context' error when trying to load a page (mvc 3).

public static ISessionFactory BuildSessionFactory()
        {

            return Fluently.Configure()
                .Database(MsSqlConfiguration.MsSql2008 // 
                              .ConnectionString(@"Server=.\SQLExpress;Database=db1;Uid=dev;Pwd=123;")
                              .ShowSql())
                //.ExposeConfiguration(c => c.SetProperty("current_session_context_class", "web"))
                //.CurrentSessionContext<CallSessionContext>()             
                .Mappings(m => m.FluentMappings
                                   .AddFromAssemblyOf<User>())
                .ExposeConfiguration(cfg => new SchemaExport(cfg)
                                                .Create(false, false))
                .BuildSessionFactory();
        }

实际的错误是在我的Repository.cs文件:

The actual error is in my Repository.cs file:

114线:公共虚拟吨得到(INT ID)
115线:{
116线:回报_sessionFactory.GetCurrentSession()获取(ID)。
117线:}
118线:

Line 114: public virtual T Get(int id) Line 115: { Line 116: return _sessionFactory.GetCurrentSession().Get(id); Line 117: } Line 118:

当我调试它,_sessionFactory不是空或任何东西,它似乎无法找到绑定的会话。

When I was debugging it, _sessionFactory wasn't null or anything, it just can't seem to find the bound session.

我有在的HttpModule我的web.config连接起来,它得到运行,因此这不是问题。

I have the httpmodule wired up in my web.config, and it does get run so that's not the problem.

在我的NHibernate的配置,我都试过:

In my nhibernate configuration, I tried both:

.ExposeConfiguration(c => c.SetProperty("current_session_context_class", "web"))

.CurrentSessionContext<CallSessionContext>()

但是,这并不能正常工作。

But that didn't work.

推荐答案

这听起来像你是不是您的会话结合上下文。请看下面的例子:

It sounds like you are not binding your session to the context. Look at the below for an example:

public class SessionFactory
{
    protected static ISessionFactory sessionFactory;
    private static ILog log = LogManager.GetLogger(typeof(SessionFactory));

    //Several functions omitted for brevity

    public static ISession GetCurrentSession()
    {
        if(!CurrentSessionContext.HasBind(GetSessionFactory()))
            CurrentSessionContext.Bind(GetSessionFactory().OpenSession());

        return GetSessionFactory().GetCurrentSession();
    }

    public static void DisposeCurrentSession()
    {
        ISession currentSession = CurrentSessionContext.Unbind(GetSessionFactory());

        currentSession.Close();
        currentSession.Dispose();
    }
}

以上的关键是,每当你找回您的第一次会议,你把它绑定到任何环境所使用。

The key to the above is that whenever you retrieve your first session you bind it to whatever context you are using.

这篇关于没有会话绑定到当前上下文的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆