NHibernate当前会话上下文问题

NHibernate当前会话上下文问题,nhibernate,fluent-nhibernate,nhibernate-3,Nhibernate,Fluent Nhibernate,Nhibernate 3,我最近已经从直接使用ISession转变为包装ISession,即工作单元类型模式 我曾经使用SQLLite(内存中)来测试这一点。我有一个简单的助手类,它配置我的SessionFactory,创建一个ISession,然后使用SchemaExport构建模式,然后返回我的ISession,模式一直存在,直到我关闭会话。我稍微改变了一下,现在配置一个SessionFactory,创建一个ISession,构建模式,并将工厂传递给我的NHibernateUnitOfWork,然后将其返回到我的测试

我最近已经从直接使用ISession转变为包装ISession,即工作单元类型模式

我曾经使用SQLLite(内存中)来测试这一点。我有一个简单的助手类,它配置我的SessionFactory,创建一个ISession,然后使用SchemaExport构建模式,然后返回我的ISession,模式一直存在,直到我关闭会话。我稍微改变了一下,现在配置一个SessionFactory,创建一个ISession,构建模式,并将工厂传递给我的NHibernateUnitOfWork,然后将其返回到我的测试

var databaseConfiguration =
                SQLiteConfiguration.Standard.InMemory()
                .Raw("connection.release_mode", "on_close")
                .Raw("hibernate.generate_statistics", "true");

            var config = Fluently.Configure().Database(databaseConfiguration).Mappings(
                m =>
                {
                    foreach (var assembly in assemblies)
                    {
                        m.FluentMappings.AddFromAssembly(assembly);
                        m.HbmMappings.AddFromAssembly(assembly);
                    }
                });

            Configuration localConfig = null;
            config.ExposeConfiguration(x =>
                {
                    x.SetProperty("current_session_context_class", "thread_static"); // typeof(UnitTestCurrentSessionContext).FullName);
                    localConfig = x;
                });

            var factory = config.BuildSessionFactory();
            ISession session = null;

            if (openSessionFunction != null)
            {
                session = openSessionFunction(factory);
            }

            new SchemaExport(localConfig).Execute(false, true, false, session.Connection, null);

            UnitTestCurrentSessionContext.SetSession(session);

            var unitOfWork = new NHibernateUnitOfWork(factory, new NHibernateUTCDateTimeInterceptor());
            return unitOfWork;
在内部,NHibernateUnitOfWork需要获取用于创建架构的ISession,否则内存中的数据库实际上没有架构,因此它调用此方法来获取ISession

private ISession GetCurrentOrNewSession()
        {
            if (this.currentSession != null)
            {
                return this.currentSession;
            }

            lock (this)
            {
                if (this.currentSession == null)
                {
                    // get an existing session if there is one, otherwise create one
                    ISession session;
                    try
                    {
                        session = this.sessionFactory.GetCurrentSession();
                    }
                    catch (Exception ex)
                    {
                        Debug.Write(ex.Message);
                        session = this.sessionFactory.OpenSession(this.dateTimeInterceptor);
                    }

                    this.currentSession = session;
                    this.currentSession.FlushMode = FlushMode.Never;
                }
            }
问题是
this.sessionFactory.GetCurrentSession
总是抛出一个异常,指出
ICurrentSessionContext
未注册

我尝试了很多不同的方法来设置属性和不同的值(如您所见,“thread_static”和我自己的
ICurrentSessionContext
),但似乎都不起作用

有人有什么建议吗

try
{
    if (NHibernate.Context.CurrentSessionContext.HasBind(sessionFactory))
    {
        session = sessionFactory.GetCurrentSession();
    }
    else
    {
        session = sessionFactory.OpenSession(this.dateTimeInterceptor);
        NHibernate.Context.CurrentSessionContext.Bind(session);
    }
}
catch (Exception ex)
{
    Debug.Write(ex.Message);
    throw;
}

这似乎奏效了,我今天晚些时候会确认并接受。谢谢您在“if”语句末尾缺少一个括号。