Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

The type initializer for 'NHibernate.Cfg.Configuration' threw an exception

Tags:

nhibernate

After upgrading from nhibernate 1.0.4.0 to nhibernate 3.3 im encountering the following error when I try to run "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: 

Any help would be greatly appreciated.

The NHibernateUtil class code is as follows :

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();
        }
}
like image 750
ramya Avatar asked Oct 08 '22 00:10

ramya


1 Answers

I had the same problem. The actual reason was i used a library that used old version of log4net. NHibernate tries to use it if find. So i had to force it to use (or actually not use) other logger by adding such line: LoggerProvider.SetLoggersFactory(new NoLoggingLoggerFactory());

like image 178
Maximal Avatar answered Nov 12 '22 13:11

Maximal