尝试实现每个Web请求的会话,没有当前的会话上下文配置

Gru*_*unf 3 nhibernate asp.net-mvc fluent-nhibernate session-per-request

正如我在标题中所说,我想实现每个Web请求的会话.我的会话提供程序配置如下(我对改变此conf不感兴趣.)

    public class SessionProvider
    {
        public static SessionProvider Instance { get; private set; }
        private static ISessionFactory _SessionFactory;

        static SessionProvider()
        {
            var provider = new SessionProvider();
            provider.Initialize();
            Instance = provider;
        }

        private SessionProvider()
        {

        }

        private void Initialize()
        {
            string csStringName = "ConnectionString";
            var cfg = Fluently.Configure()
                ....ommiting mappings and db conf.
                .ExposeConfiguration(c => c.SetProperty("current_session_context_class", "web"))
                .BuildConfiguration();
            _SessionFactory = cfg.BuildSessionFactory();    
        }

        public ISession OpenSession()
        {
            return _SessionFactory.OpenSession();
        }

        public ISession GetCurrentSession()
        {
            return _SessionFactory.GetCurrentSession();
        }
    }
Run Code Online (Sandbox Code Playgroud)

在Global.asax.cs中,我有以下与每个Web请求的会话相关的代码.

private static ISessionFactory SessionFactory { get; set; }

    protected void Application_Start()
    {
        SessionFactory = MyDomain.SessionProvider.Instance.OpenSession().SessionFactory;

        AreaRegistration.RegisterAllAreas();

        RegisterGlobalFilters(GlobalFilters.Filters);
        RegisterRoutes(RouteTable.Routes);

    }

    protected void Application_BeginRequest(object sender, EventArgs e)
    {
        var session = SessionFactory.OpenSession();
        CurrentSessionContext.Bind(session);
    }

    protected void Application_EndRequest(object sender, EventArgs e)
    {
        var session = CurrentSessionContext.Unbind(SessionFactory);
        session.Dispose();
    }
Run Code Online (Sandbox Code Playgroud)

在debuggin我的webapp上,我收到错误:没有配置当前的会话上下文. ErrorMessage引用global.asax行CurrentSessionContext.Bind(session);

更新已 添加.ExposeConfiguration(c => c.SetProperty("current_session_context_class", "web")) ,现在我有从我的控制器检索数据的错误消息,如此 错误消息:会话已关闭!对象名称:'ISession'.

控制器代码:

using (ISession session = SessionProvider.Instance.GetCurrentSession())
            {
                using (ITransaction transaction = session.BeginTransaction())
                {
                    data = session.QueryOver<Object>()
                       ...ommiting

                    transaction.Commit();
                    return PartialView("_Partial", data);
                }

            }    
Run Code Online (Sandbox Code Playgroud)

Col*_*e W 5

第一个问题

您需要在nhibernate配置部分中对其进行配置:

<property name="current_session_context_class">web</property>
Run Code Online (Sandbox Code Playgroud)

我目前也做这样的事情:

if (!CurrentSessionContext.HasBind(SessionFactory))
{
    CurrentSessionContext.Bind(SessionFactory.OpenSession());
}
Run Code Online (Sandbox Code Playgroud)

第二个问题

请看下面的文章,以流畅的方式修改配置:currentsessioncontext流利的nhibernate如何做到这一点?

第三个问题

您正在关闭两次会话.

using (ISession session = SessionProvider.Instance.GetCurrentSession()) 
Run Code Online (Sandbox Code Playgroud)

关闭你的会话.然后你再次进行了Application_EndRequest.如果您有其他问题,请发布一个新问题.