【问题标题】:trying to implement session per web request, No current session context configure尝试根据 Web 请求实现会话,没有配置当前会话上下文
【发布时间】:2012-05-21 18:31:31
【问题描述】:

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

    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 中,我有以下与每个网络请求的会话相关的代码。

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

在调试我的 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);
                }

            }    

【问题讨论】:

    标签: asp.net-mvc nhibernate fluent-nhibernate session-per-request


    【解决方案1】:

    第一个问题

    你需要在你的 nhibernate 配置部分配置它:

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

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

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

    第二个问题

    请看以下文章流畅修改配置:currentsessioncontext fluent nhibernate how to do it?

    第三个问题

    您将关闭会话两次。

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

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

    【讨论】:

    • 我正在使用流畅的配置。这些建议的代码我应该放在哪里?
    • 感谢您的宝贵时间。现在我在检索数据时关闭了会话。详细信息在更新的问题中。
    • 您正在关闭会话两次。 using (ISession session = SessionProvider.Instance.GetCurrentSession()) 关闭您的会话。然后你在Application_EndRequest 又做了一次。如果您还有其他问题,请发布一个新问题。
    • 当然。谢谢。发布作为我问题的答案,因为我愿意接受您的回答。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-10-16
    • 2010-12-24
    • 1970-01-01
    相关资源
    最近更新 更多