6

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();
        }
}
ramya
  • 113
  • 2
  • 2
  • 7
  • Please show us what you have at `NHibernateUtil.cs:line 24`. It's not an NHibernate file, it's one of your own project. The TypeInitializer exception is because the exception is raised from a (static) constructor. It's generally best to avoid raising exceptions from constructors. – Abel Apr 25 '12 at 12:50
  • 1
    Is your log4net assembly output to the bin folder of your application? – Vadim Apr 25 '12 at 16:29
  • yes I have the log4net assembly in the bin folder – ramya Apr 26 '12 at 06:08
  • looks like there is a call to get a logger in a generated assembly. are you using reflection.emit somewhere? – Firo Apr 26 '12 at 10:12

3 Answers3

5

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());

Maximal
  • 51
  • 1
  • 2
1

Not sure why it's not working, but I'd just replace

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

with

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

where Entity is some class that exists in the assembly of your mapping files.

Vadim
  • 17,897
  • 4
  • 38
  • 62
-2

For the benefit of others who might find this question via Google:

For us, this error was a red-herring. Our app ran fine until we deployed a new component AND it would fail (in an unknown way) AND IIS would recycle the app pool. The problem was an HTML to JPG component we were using was erroring somehow and causing all of our w3wp.exe worker processes to consume maximum CPU. When the app pool was recycled via IIS, the entire site would go down and NHibernate would throw this error continuously until an iisreset. Before the recycle, the site would still be very responsive even with the CPU load.

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.

Tyler Forsythe
  • 1,471
  • 17
  • 22