带有.net 4上的Spring的Nhibernate 2.1抛出System.ExecutionEngineeException

带有.net 4上的Spring的Nhibernate 2.1抛出System.ExecutionEngineeException,nhibernate,.net-4.0,executionengineexception,Nhibernate,.net 4.0,Executionengineexception,我有一个在.NET3.5上运行NHibernate2.1.0.4000的网站。我们使用spring作为代理工厂 一切正常。我已尝试使用向导将项目升级到.Net 4.0。一切进展顺利 但是,当代码试图对Nhibernate执行任何操作时,我得到一个非常不友好的System.ExecutionEngineeException异常。没有堆栈跟踪,也没有内部异常 我们使用的是一个NhibernateHelper类(如下),我已经对它进行了修改,会话配置正常(无异常)。与.NET3.5版本相比,细节没有任

我有一个在.NET3.5上运行NHibernate2.1.0.4000的网站。我们使用spring作为代理工厂

一切正常。我已尝试使用向导将项目升级到.Net 4.0。一切进展顺利

但是,当代码试图对Nhibernate执行任何操作时,我得到一个非常不友好的System.ExecutionEngineeException异常。没有堆栈跟踪,也没有内部异常

我们使用的是一个NhibernateHelper类(如下),我已经对它进行了修改,会话配置正常(无异常)。与.NET3.5版本相比,细节没有任何变化

第一次尝试从数据库中获取某些内容失败

会话在请求开始时在处理程序中打开(下面未显示)。 我们也在使用unity,它是在应用程序启动时设置的(不确定是否有任何关系)

第一个打给Nhibernate的电话是

var emp = NHibernateHelper.CurrentSession.Get<SMS.DomainModel.Employee>(-200694);
var emp=NHibernateHelper.CurrentSession.Get(-200694);
我只是想要一条有意义的错误信息,让我继续下去

我试着查看NhibernateProfiler,注册的只是会话的开始

非常感谢您的帮助

NhibernateHelper类如下

using System;
using System.Configuration;
using System.IO;
using System.Reflection;
using FluentNHibernate;
using FluentNHibernate.Cfg;
using HibernatingRhinos.NHibernate.Profiler.Appender;
using log4net;
using Microsoft.Practices.EnterpriseLibrary.Common.Configuration;
using Microsoft.Practices.EnterpriseLibrary.Data.Configuration;
using NHibernate;
using NHibernate.Cfg;
using NHibernate.Event;
using NHibernate.Tool.hbm2ddl;
using SMS.Infrastructure.Persistence.Logging;
using SMS.Infrastructure.Persistence.Mappings;
using Configuration=NHibernate.Cfg.Configuration;
using System.Data.SqlClient;

namespace SMS.Infrastructure.Persistence
{
    public static class NHibernateHelper
    {
        static readonly ILog Log = LogManager.GetLogger(typeof(NHibernateHelper));
        static Configuration configuration;

        public static ISessionFactory SessionFactory
        {
            get
            {
                return Singleton.sessionFactory;
            }
        }

        // Lazy singleton pattern from http://www.yoda.arachsys.com/csharp/singleton.html
        class Singleton
        {
            static Singleton() { }

            internal static readonly ISessionFactory sessionFactory = CreateSessionFactory();
        }

        public static ISession OpenSession()
        {
            return SessionFactory.OpenSession();
        }

        public static ISession CurrentSession
        {
            get { return SessionFactory.GetCurrentSession(); }
        }

        static ISessionFactory CreateSessionFactory()
        {
            try
            {

                Log.Info("Creating NHibernate session factory");

                NHibernateProfiler.Initialize();

                configuration = new Configuration();

                try
                {
                    // Try to configure NHibernate automagically...
                    configuration.Configure();
                }
                catch (HibernateConfigException e)
                {
                    if (e.InnerException is IOException)
                    {
                        // Otherwise specify a file name manually.
                        configuration.Configure("hibernate.cfg.xml");
                    }
                    else
                        throw;
                }

                Log.Info("Registering custom SMS event listeners");

                RegisterCustomListeners(configuration);

                // Has someone specified a default_schema? No? try and guess
                // it from the (Enterprise Library :/ ) query string.
                if (!configuration.Properties.ContainsKey("default_schema"))
                {
                    Log.Info("Setting default schema");
                    configuration.SetProperty("default_schema", GetDefaultSchema());
                }

                ISessionFactory sessionFactory = Fluently.Configure(configuration)
                    .Mappings(m =>
                    {
                        m.HbmMappings.AddFromAssemblyOf<BusinessUnitTypeMapping>();
                        m.FluentMappings.AddFromAssemblyOf<BusinessUnitTypeMapping>();

                    })
                    .BuildSessionFactory();

                Log.Info("Session factory was configured successfully");

                return sessionFactory;
            }
            catch (Exception ex)
            {
                throw new ArgumentNullException(ex.ToString());
            }
        }

        /// <summary>
        /// NHibernate allows custom event listeners to be registered in 
        /// config files or programmatically. Due to the re-use of configs in
        /// SMS, we chose to do it via code.
        /// 
        /// This is how we extend NHibernate for SMS, and this is why
        /// NHibernate is the best ORM..!
        /// </summary>
        static void RegisterCustomListeners(Configuration config)
        {
            if (config == null)
                throw new ArgumentNullException("config");

            // Event listeners for audit logging.
            //config.SetListener(ListenerType.PreInsert, new AuditEventListener());
            //config.SetListener(ListenerType.PreUpdate, new AuditEventListener());
            //config.SetListener(ListenerType.PreDelete, new AuditEventListener());

            // Event listener for wiring up .NET events between parent/child
            // objects, and the intermediary mapping for Tasks.
            //
            // Warning: be careful with the order in which these listeners are
            // added!!!
            //
            // BindEventsOnLoadListener must come before 
            // TaskAddresseeLoadEventListener for example otherwise OSM Task
            // Decorators don't attach properly.
            config.SetListeners(ListenerType.PostLoad, new IPostLoadEventListener[]
                {
                    new BindEventsOnLoadListener(),
                    new TaskAddresseeLoadEventListener()
                });

        }

        /// <summary>
        /// Optional step: destroy and re-create the database scheme based on
        /// the mapping files. Gives you a totally clean database in between
        /// testing each fixture.
        /// </summary>
        public static void ExportDatabaseSchema(string fileName)
        {
            if (configuration == null)
                CreateSessionFactory();

            Log.InfoFormat("Exporting DDL to {0}", fileName);

            var exporter = new SchemaExport(configuration);
            exporter.SetOutputFile(fileName);
            exporter.Execute(true, false, false);
        }

        public static void ExportFluentMappings(string directoryName)
        {
            Log.InfoFormat("Exporting fluent mappings to {0}", directoryName);
            var model = new PersistenceModel();
            model.AddMappingsFromAssembly(Assembly.GetAssembly(typeof(BusinessUnitTypeMapping)));
            model.WriteMappingsTo(directoryName);
        }

        /// <summary>
        /// hibernate's default_schema is worked out programmatically from the
        /// Enterprise Library connection string. E.g.  
        /// Initial Catalog=OSM2Tests  -->  default_schema = SMS2Tests.dbo
        /// </summary>
        public static string GetDefaultSchema()
        {
            try
            {
                DatabaseSettings settings = DatabaseSettings.GetDatabaseSettings(new SystemConfigurationSource());
                var connectionstring = ConfigurationManager.ConnectionStrings[settings.DefaultDatabase].ConnectionString;
                var initialCatalog = new SqlConnectionStringBuilder(connectionstring).InitialCatalog;
                return String.Format("{0}.dbo", initialCatalog);
            }
            catch (Exception)
            {
                throw new Exception("Could not get default schema from connection string.");
            }
        }
    }
}
使用系统;
使用系统配置;
使用System.IO;
运用系统反思;
使用氟纤维酸盐;
使用FluentNHibernate.Cfg;
使用hibertingrhinos.NHibernate.Profiler.Appender;
使用log4net;
使用Microsoft.Practices.EnterpriseLibrary.Common.Configuration;
使用Microsoft.Practices.EnterpriseLibrary.Data.Configuration;
使用NHibernate;
使用NHibernate.Cfg;
使用NHibernate.Event;
使用NHibernate.Tool.hbm2ddl;
使用SMS.Infrastructure.Persistence.Logging;
使用SMS.Infrastructure.Persistence.Mappings;
使用Configuration=NHibernate.Cfg.Configuration;
使用System.Data.SqlClient;
命名空间SMS.Infrastructure.Persistence
{
公共静态类NHibernateHelper
{
静态只读ILog Log=LogManager.GetLogger(typeof(NHibernateHelper));
静态配置;
公共静态ISessionFactory会话工厂
{
得到
{
返回Singleton.sessionFactory;
}
}
//来自http://www.yoda.arachsys.com/csharp/singleton.html
单件阶级
{
静态单例(){}
内部静态只读ISessionFactory sessionFactory=CreateSessionFactory();
}
公共静态会话OpenSession()
{
返回SessionFactory.OpenSession();
}
公共静态会话当前会话
{
获取{return SessionFactory.GetCurrentSession();}
}
静态ISessionFactory CreateSessionFactory()
{
尝试
{
Log.Info(“创建NHibernate会话工厂”);
NHibernateProfiler.Initialize();
配置=新配置();
尝试
{
//尝试自动配置NHibernate。。。
Configure.Configure();
}
捕获(HibernateConfigeException e)
{
如果(例如,InnerException是IOException)
{
//否则,请手动指定文件名。
Configure(“hibernate.cfg.xml”);
}
其他的
投掷;
}
Log.Info(“注册自定义SMS事件侦听器”);
RegisterCustomListeners(配置);
//是否有人指定了默认的_模式?否?请尝试猜测
//它来自(企业库:/)查询字符串。
if(!configuration.Properties.ContainsKey(“默认_架构”))
{
Log.Info(“设置默认模式”);
SetProperty(“default_schema”,GetDefaultSchema());
}
ISessionFactory sessionFactory=流畅。配置(配置)
.Mappings(m=>
{
m、 HbmMappings.AddFromAssemblyOf();
m、 FluentMappings.AddFromAssemblyOf();
})
.BuildSessionFactory();
Log.Info(“会话工厂配置成功”);
返回工厂;
}
捕获(例外情况除外)
{
抛出新ArgumentNullException(例如ToString());
}
}
/// 
///NHibernate允许在中注册自定义事件侦听器
///配置文件或以编程方式。由于在
///短信,我们选择通过代码。
/// 
///这就是我们为SMS扩展NHibernate的方式,这就是为什么
///NHibernate是最好的ORM。。!
/// 
静态无效注册表自定义侦听器(配置)
{
if(config==null)
抛出新的ArgumentNullException(“配置”);
//用于审核日志记录的事件侦听器。
//config.SetListener(ListenerType.PreInsert,新的AuditEventListener());
//config.SetListener(ListenerType.PreUpdate,新的AuditEventListener());
//config.SetListener(ListenerType.PreDelete,新的AuditEventListener());
//用于在父级/子级之间连接.NET事件的事件侦听器
//对象和任务的中间映射。
//
//警告:请注意这些侦听器的排列顺序
//添加!!!
//
//BindEventsOnLoadListener必须在
//TaskAddresseeLoadEventListener,例如OSM任务
//装潢师不喜欢