C# global.asax应用程序中的NHibernate会话\u BeginRequest

C# global.asax应用程序中的NHibernate会话\u BeginRequest,c#,wcf,nhibernate,C#,Wcf,Nhibernate,我正在尝试在Global.asax和Application_BeginRequest中启动hibernate会话,然后访问Global.asax上的静态会话工厂,以获取WCF服务中的当前会话 但是,当我尝试获取服务中的当前会话时,我会得到“对象引用未设置为对象的实例”。我正在使用basicHttpBinding访问服务 Global.asax public class Global : System.Web.HttpApplication { public static ISes

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

但是,当我尝试获取服务中的当前会话时,我会得到“对象引用未设置为对象的实例”。我正在使用basicHttpBinding访问服务

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

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>

NHibernate.Connection.DriverConnectionProvider
NHibernate.dial.MsSql2012Dialect
NHibernate.Driver.SqlClientDriver
...
网状物
看到以下内容后,我将
web
更改为
call

而且它正在工作

为什么您总是在EndRequest中回滚事务?不会保存任何内容。如果有尚未提交的事务仍处于活动状态,则将回滚该事务。这是在交易期间发生未捕获异常的情况下发生的。否则没有未提交的事务,会话将被刷新。如果您在Web中托管,那么您的提交逻辑应该在其中。Web请求是工作单元。在代码中不应该有显式的commit调用。还有一个您没有使用的wcf上下文类。