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

2024-03-29

正如我在标题中所说,我想为每个网络请求实现会话。 我的会话提供程序是这样配置的(我对更改此配置不感兴趣。)

    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();
        }
    }

在 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();
    }

在调试我的 web 应用程序时,我收到错误:未配置当前会话上下文。ErrorMessage 引用了 global.asax 行 CurrentSessionContext.Bind(session);

Updated Added .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);
                }

            }    

第一个问题

您需要在 nhibernate 配置部分中配置它:

<property name="current_session_context_class">web</property>

我目前也在做这样的事情:

if (!CurrentSessionContext.HasBind(SessionFactory))
{
    CurrentSessionContext.Bind(SessionFactory.OpenSession());
}

第二个问题

请看下面文章流畅修改配置:currentsessioncontext流畅nhibernate怎么办呢? https://stackoverflow.com/questions/4702703/currentsessioncontext-fluent-nhibernate-how-to-do-it

第三个问题

您将两次关闭会话。

using (ISession session = SessionProvider.Instance.GetCurrentSession()) 

关闭您的会话。然后你又做了一次Application_EndRequest。如果您还有其他问题,请发布新问题。

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

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

随机推荐