没有绑定到当前上下文的会话(与url冲突)

时间:2014-02-13 06:23:27

标签: session nhibernate asp.net-mvc-2 web-config

我有域名xyz.com的网站。当我输入网址为www.xyz.com时,它会将错误称为“没有会话绑定到当前上下文”。但是,当我只键入xyz.com时,我能够看到我的网站。 我在我的代码中使用了NHibernate。 我已将此link作为参考,但问题仍然存在。

以下是我的代码:

public class MvcApplication : System.Web.HttpApplication
    {
        private static ISessionFactory _sessionFactory;

        public MvcApplication()
        {
            BeginRequest += new EventHandler(MvcApplication_BeginRequest);
            EndRequest += new EventHandler(MvcApplication_EndRequest);
        }

        public class AddNoLockHintsInterceptor : EmptyInterceptor
        {
            public override SqlString OnPrepareStatement(SqlString sql)
            {
                // Modify the sql to add hints

                return sql;
            }
        }

        protected void MvcApplication_BeginRequest(object sender, EventArgs e)
        {
            var session = SessionFactory.OpenSession(new AddNoLockHintsInterceptor());
            CurrentSessionContext.Bind(session);
            session.BeginTransaction(IsolationLevel.ReadUncommitted);
        }

        protected void MvcApplication_EndRequest(object sender, EventArgs e)
        {
            var session = SessionFactory.GetCurrentSession();
            if (session.Transaction != null && session.Transaction.IsActive)
            {
                session.Transaction.Commit();
            }
            var _session = CurrentSessionContext.Unbind(MvcApplication.SessionFactory);
            _session.Close();
        }


        public static ISessionFactory SessionFactory
        {
            get { return _sessionFactory; }
        }


        private static ISessionFactory CreateSessionFactory()
        {
            return Fluently.Configure()
              .Database(MsSqlConfiguration.MsSql2008.ConnectionString(c => c.FromConnectionStringWithKey("ElearnConnection")))
              .Mappings(m => m.FluentMappings.AddFromAssemblyOf<CMSContentMap>())
              .ExposeConfiguration(cfg => cfg.SetProperty("current_session_context_class", "web").SetProperty("adonet.batch_size", "100"))
              .BuildSessionFactory();
        }



        private static void BuildSchema(Configuration config)
        {
            // delete the existing db on each run
            if (System.IO.File.Exists(@"C:/schema.sql"))
                System.IO.File.Delete(@"C:/schema.sql");


            new SchemaExport(config).SetOutputFile(@"C:/schema.sql").Execute(true, false, false);
        }

        protected void Application_Start()
        {
            _sessionFactory = MvcApplication.CreateSessionFactory();
            AreaRegistration.RegisterAllAreas();

        }

    }

Webconfig文件我添加了hibernate-configuration部分:

<configuration>
    <configSections>
      <section name="hibernate-configuration" type="NHibernate.Cfg.ConfigurationSectionHandler, NHibernate" />
    </configSections>
    <hibernate-configuration xmlns="urn:nhibernate-configuration-2.2">
        <session-factory>
            <property name="dialect">NHibernate.Dialect.MsSql2008Dialect</property>
            <property name="connection.provider">NHibernate.Connection.DriverConnectionProvider</property>
            <property name="connection.connection_string">Server=myserver;initial catalog=dbName;User ID=xyz;Password=xyz;Integrated Security=False</property>
            <property name="proxyfactory.factory_class">NHibernate.ByteCode.LinFu.ProxyFactoryFactory, NHibernate.ByteCode.LinFu</property>

            <mapping assembly="QuickStart" />
        </session-factory>
    </hibernate-configuration>

0 个答案:

没有答案
相关问题