global.asax中的NHibernate会话Application_BeginRequest [英] NHibernate Session in global.asax Application_BeginRequest

查看:73
本文介绍了global.asax中的NHibernate会话Application_BeginRequest的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试在Global.asax和Application_BeginRequest中启动休眠会话,然后在Global.asax上访问静态SessionFactory以在WCF服务中获取当前会话.

I am trying to start a hibernate session in Global.asax and Application_BeginRequest and then access a static SessionFactory on Global.asax to get the current session in a WCF service.

但是,当我尝试在服务中获取当前会话时,出现对象引用未设置为对象实例"的信息.我正在使用basicHttpBinding访问该服务.

However I get "Object reference not set to an instance of an object" when I try to get the current session inside the service. I am accessing the service using basicHttpBinding.

Global.asax

Global.asax

public class Global : System.Web.HttpApplication
{
        public static ISessionFactory SessionFactory { get; private set; }

        protected void Application_Start(object sender, EventArgs e)
        {
            //Initialize session factory and set up mappings
        }


        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);
            if (session != null)
            {
                if (session.Transaction != null &&
                    session.Transaction.IsActive)
                {
                    session.Transaction.Rollback();
                }
                else
                    session.Flush();
                session.Close();
            }
        }

}

MyService.svc

MyService.svc

public class MyService : IMyService
{
   public void doStuff()
   {
      //Exception occurs here. Session Factory is not null. But GetCurrentSession() gives exception.
      ISession session = Global.SessionFactory.GetCurrentSession();

   }
} 

休眠配置

  <hibernate-configuration xmlns="urn:nhibernate-configuration-2.2">
    <session-factory>
      <property name="connection.provider">NHibernate.Connection.DriverConnectionProvider</property>
      <property name="dialect">NHibernate.Dialect.MsSql2012Dialect</property>
      <property name="connection.driver_class">NHibernate.Driver.SqlClientDriver</property>
      <property name="connection.connection_string">...</property>
      <property name="current_session_context_class">web</property>
    </session-factory>
  </hibernate-configuration>

推荐答案

看到以下内容后,我将<property name="current_session_context_class">web</property>更改为<property name="current_session_context_class">call</property>:

I changed <property name="current_session_context_class">web</property> to <property name="current_session_context_class">call</property> after seeing this: https://groups.google.com/forum/#!topic/nhusers/jo9TJOKXWlI and it is working

这篇关于global.asax中的NHibernate会话Application_BeginRequest的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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