6

从 nhibernate 1.0.4.0 升级到 nhibernate 3.3 后,当我尝试运行“Configuration cfg = new Configuration();”时遇到以下错误

System.TypeInitializationException was caught
  Message="The type initializer for 'NHibernate.Cfg.Configuration' threw an exception."
  Source="NHibernate"
  TypeName="NHibernate.Cfg.Configuration"
  StackTrace:
       at NHibernate.Cfg.Configuration..ctor()
       at KEH.Web.Data.NHibernateUtil..cctor() in F:\Projects\KEH nHibernate\KEHWeb\Data\Data\NHibernateUtil.cs:line 24
  InnerException: System.NotSupportedException
       Message="The invoked member is not supported in a dynamic assembly."
       Source="mscorlib"
       StackTrace:
            at System.Reflection.Emit.AssemblyBuilder.get_Location()
            at log4net.Util.SystemInfo.AssemblyLocationInfo(Assembly myAssembly)
            at log4net.Core.DefaultRepositorySelector.GetInfoForAssembly(Assembly assembly, String& repositoryName, Type& repositoryType)
            at log4net.Core.DefaultRepositorySelector.CreateRepository(Assembly repositoryAssembly, Type repositoryType, String repositoryName, Boolean readAssemblyAttributes)
            at log4net.Core.DefaultRepositorySelector.CreateRepository(Assembly repositoryAssembly, Type repositoryType)
            at log4net.Core.DefaultRepositorySelector.GetRepository(Assembly repositoryAssembly)
            at log4net.Core.LoggerManager.GetLogger(Assembly repositoryAssembly, String name)
            at log4net.LogManager.GetLogger(Assembly repositoryAssembly, String name)
            at log4net.LogManager.GetLogger(Type type)
            at lambda_method(ExecutionScope , Type )
            at NHibernate.Log4NetLoggerFactory.LoggerFor(Type type)
            at NHibernate.LoggerProvider.LoggerFor(Type type)
            at NHibernate.Cfg.Configuration..cctor()
       InnerException: 

任何帮助将不胜感激。

NHibernateUtil 类代码如下:

public class NHibernateUtil
    {
        private static readonly Configuration cfg;
        private static readonly ISessionFactory sessionFactory;
        private static readonly ILog logger = LogManager.GetLogger(MethodBase.GetCurrentMethod().DeclaringType);

        static NHibernateUtil()
        {
            try
            {
                logger.Debug("Before Initializing NHibernate");
                cfg  = new Configuration();
                cfg.AddAssembly("KEH.Web.Data");
                sessionFactory = cfg.BuildSessionFactory();
                logger.Debug("Initialized NHibernate");
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.Message);
            }
        }

        public static ISession OpenSession()
        {
            logger.Debug("Before Getting Connection");
            return sessionFactory.OpenSession();
        }
}
4

3 回答 3

5

我有同样的问题。实际原因是我使用了一个使用旧版本 log4net 的库。如果找到,NHibernate 会尝试使用它。所以我不得不通过添加这样的行来强制它使用(或实际上不使用)其他记录器: LoggerProvider.SetLoggersFactory(new NoLoggingLoggerFactory());

于 2013-03-04T13:56:06.407 回答
1

不知道为什么它不起作用,但我只是更换

cfg.AddAssembly("KEH.Web.Data");

cfg.AddAssembly(typeof(Entity).Assembly);

其中 Entity 是映射文件程序集中存在的某个类。

于 2012-04-26T17:06:16.443 回答
-2

为了其他可能通过 Google 找到此问题的人的利益:

对我们来说,这个错误是一个红鲱鱼。我们的应用程序运行良好,直到我们部署了一个新组件并且它会失败(以未知方式)并且 IIS 会回收应用程序池。问题是我们使用的 HTML 到 JPG 组件以某种方式出错并导致我们所有的 w3wp.exe 工作进程消耗最大 CPU。当应用程序池通过 IIS 回收时,整个站点将关闭,NHibernate 会不断抛出此错误,直到 iisreset。在回收之前,即使有 CPU 负载,站点仍然会非常敏感。

While we still don't know how the component was failing or why it was cascading to problems with NHibernate initializing, the point is it was a red-herring. Be sure to watch for this error "suddenly" occurring shortly after a new deployment, and keep logs of your CPU utilization so it can help spot when trouble is brewing. Finally, if the downtime is happening near the same time every day, it's probably an automatic IIS app pool recycle, and that should be another clue that something is bugging out your application and surfacing during the recycle.

Ultimately, we disabled the HTML to JPG component until a workaround can be found and our up-time sprung back to 100%.

Hope this helps someone.

于 2013-12-31T22:01:44.053 回答