试图实现每个Web请求会话,没有当前会话的上下文配置 [英] trying to implement session per web request, No current session context configure

查看:174
本文介绍了试图实现每个Web请求会话,没有当前会话的上下文配置的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在标题我想实现每个Web请求会话说。
我的会话提供配置是这样的(我不感兴趣,在改变这个配置)

 公共类SessionProvider
    {
        公共静态SessionProvider实例{搞定;私人集; }
        私有静态ISessionFactory _SessionFactory;        静态SessionProvider()
        {
            VAR提供商=新SessionProvider();
            provider.Initialize();
            实例=供应商;
        }        私人SessionProvider()
        {        }        私人无效初始化()
        {
            字符串csStringName =ConnectionString的;
            VAR CFG = Fluently.Configure()
                .... ommiting映射和数据库的conf。
                .ExposeConfiguration(C => c.SetProperty(current_session_context_class,网络))
                .BuildConfiguration();
            _SessionFactory = cfg.BuildSessionFactory();
        }        公众的ISession的openSession()
        {
            返回_SessionFactory.OpenSession();
        }        公众的ISession的getCurrentSession()
        {
            返回_SessionFactory.GetCurrentSession();
        }
    }

里面的Global.asax.cs我有以下每个Web REQ相关会议code。

 私有静态ISessionFactory的SessionFactory {搞定;组; }    保护无效的Application_Start()
    {
        SessionFactory的= MyDomain.SessionProvider.Instance.OpenSession()SessionFactory的。        AreaRegistration.RegisterAllAreas();        RegisterGlobalFilters(GlobalFilters.Filters);
        的RegisterRoutes(RouteTable.Routes);    }    保护无效的Application_BeginRequest(对象发件人,EventArgs的发送)
    {
        VAR会话= SessionFactory.OpenSession();
        CurrentSessionContext.Bind(会话);
    }    保护无效Application_EndRequest(对象发件人,EventArgs的发送)
    {
        VAR会话= CurrentSessionContext.Unbind(SessionFactory的);
        session.Dispose();
    }

在debuggin我的web应用程序,我得到错误:否当前会话的上下文配置即可。
的ErrorMessage参考的Global.asax线CurrentSessionContext.Bind(会话);

更新
新增 .ExposeConfiguration(C => c.SetProperty(current_session_context_class,网络))
现在我有错误消息从我的控制器这样的检索数据
错误信息:会话关闭!对象名称:'ISession的

控制器code:

 使用(ISession的会话= SessionProvider.Instance.GetCurrentSession())
            {
                使用(ITransaction事务= session.BeginTransaction())
                {
                    数据= session.QueryOver<对象>()
                       ... ommiting                    器transaction.commit();
                    返回PartialView(_部分,数据);
                }            }


解决方案

第1个问题

您需要配置它在你的NHibernate的配置部分:

 <属性名=current_session_context_class>网络< /性>

此外,我目前正在做这样的事情:

 如果(!CurrentSessionContext.HasBind(SessionFactory的))
{
    CurrentSessionContext.Bind(SessionFactory.OpenSession());
}

第二个问题

请看看下面的文章流利修改配置:<一href=\"http://stackoverflow.com/questions/4702703/currentsessioncontext-fluent-nhibernate-how-to-do-it\">currentsessioncontext连贯NHibernate怎么办呢?

第3问题

您在两次关闭您的会话。

 使用(ISession的会话= SessionProvider.Instance.GetCurrentSession())

关闭您的会话。然后你再次做到了在 Application_EndRequest 。如果您有其他问题,请张贴新的问题。

As I said in the title I want to implement session per web request. My session provider is configured like this (I'm not interested in changing this 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();
        }
    }

Inside Global.asax.cs I have following code related to session per web req.

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

On debuggin my webapp I get error: No current session context configured. ErrorMessage referenced to global.asax line CurrentSessionContext.Bind(session);

Updated Added .ExposeConfiguration(c => c.SetProperty("current_session_context_class", "web")) and now I have error message on retrieving data from my controller like this Error message: Session is closed! Object name: 'ISession'.

Controller code:

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

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

            }    

解决方案

1st Question

You need to configure it in your nhibernate configuration section:

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

Also I currently do something like this:

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

2nd Question

Please look at the following article to modify configuration fluently: currentsessioncontext fluent nhibernate how to do it?

3rd Question

You are closing your session twice.

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

closes your session. Then you did it again in Application_EndRequest. If you have another question please post a new question.

这篇关于试图实现每个Web请求会话,没有当前会话的上下文配置的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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