title: Creating a proxy instance failed with COMException (IIS, Windows Server 2012, NHibernate)
body: <p>Been running a site for 4+ years on NHibernate. We recently moved the site to a Windows Server 2012 machine and updated NHibernate to 3.3.
  After about a month on the new server we began to see the following exceptions</p>
  
  <pre><code>NHibernate.HibernateException: Creating a proxy instance failed ---&gt; System.Runtime.InteropServices.COMException: Class not registered (Exception from HRESULT: 0x80040154 (REGDB_E_CLASSNOTREG))
  at System.Reflection.Emit.AssemblyBuilder.DefineDynamicModule(RuntimeAssembly containingAssembly, Boolean emitSymbolInfo, String name, String filename, StackCrawlMarkHandle stackMark, IntPtr&amp; pInternalSymWriter, ObjectHandleOnStack retModule, Boolean fIsTransient, Int32&amp; tkFile)
  at System.Reflection.Emit.AssemblyBuilder.DefineDynamicModuleInternalNoLock(String name, String fileName, Boolean emitSymbolInfo, StackCrawlMark&amp; stackMark)
  at System.Reflection.Emit.AssemblyBuilder.DefineDynamicModuleInternal(String name, String fileName, Boolean emitSymbolInfo, StackCrawlMark&amp; stackMark)
  at System.Reflection.Emit.AssemblyBuilder.DefineDynamicModule(String name, String fileName, Boolean emitSymbolInfo)
  </code></pre>
  
  <p>A restart of the application pool solves the problem for a while.</p>
  
  <p>It happens about once a week, it affects different entities everytime, and other parts of the site (that also uses NHibernate) continues to work.</p>
  
  <p>Full stacktrace below:</p>
  
  <pre><code>NHibernate.HibernateException: Creating a proxy instance failed ---&gt; System.Runtime.InteropServices.COMException: Class not registered (Exception from HRESULT: 0x80040154 (REGDB_E_CLASSNOTREG))
  at System.Reflection.Emit.AssemblyBuilder.DefineDynamicModule(RuntimeAssembly containingAssembly, Boolean emitSymbolInfo, String name, String filename, StackCrawlMarkHandle stackMark, IntPtr&amp; pInternalSymWriter, ObjectHandleOnStack retModule, Boolean fIsTransient, Int32&amp; tkFile)
  at System.Reflection.Emit.AssemblyBuilder.DefineDynamicModuleInternalNoLock(String name, String fileName, Boolean emitSymbolInfo, StackCrawlMark&amp; stackMark)
  at System.Reflection.Emit.AssemblyBuilder.DefineDynamicModuleInternal(String name, String fileName, Boolean emitSymbolInfo, StackCrawlMark&amp; stackMark)
  at System.Reflection.Emit.AssemblyBuilder.DefineDynamicModule(String name, String fileName, Boolean emitSymbolInfo)
  at NHibernate.Proxy.DynamicProxy.DefaultProxyAssemblyBuilder.DefineDynamicModule(AssemblyBuilder assemblyBuilder, String moduleName)
  at NHibernate.Proxy.DynamicProxy.ProxyFactory.CreateUncachedProxyType(Type baseType, Type[] baseInterfaces)
  at NHibernate.Proxy.DynamicProxy.ProxyFactory.CreateProxyType(Type baseType, Type[] interfaces)
  at NHibernate.Proxy.DynamicProxy.ProxyFactory.CreateProxy(Type instanceType, IInterceptor interceptor, Type[] baseInterfaces)
  at NHibernate.Proxy.DefaultProxyFactory.GetProxy(Object id, ISessionImplementor session)
  --- End of inner exception stack trace ---
  at NHibernate.Proxy.DefaultProxyFactory.GetProxy(Object id, ISessionImplementor session)
  at NHibernate.Tuple.Entity.AbstractEntityTuplizer.CreateProxy(Object id, ISessionImplementor session)
  at NHibernate.Persister.Entity.AbstractEntityPersister.CreateProxy(Object id, ISessionImplementor session)
  at NHibernate.Event.Default.DefaultLoadEventListener.CreateProxyIfNecessary(LoadEvent event, IEntityPersister persister, EntityKey keyToLoad, LoadType options, IPersistenceContext persistenceContext)
  at NHibernate.Event.Default.DefaultLoadEventListener.ProxyOrLoad(LoadEvent event, IEntityPersister persister, EntityKey keyToLoad, LoadType options)
  at NHibernate.Event.Default.DefaultLoadEventListener.OnLoad(LoadEvent event, LoadType loadType)
  at NHibernate.Impl.SessionImpl.FireLoad(LoadEvent event, LoadType loadType)
  at NHibernate.Impl.SessionImpl.InternalLoad(String entityName, Object id, Boolean eager, Boolean isNullable)
  at NHibernate.Type.EntityType.ResolveIdentifier(Object id, ISessionImplementor session)
  at NHibernate.Type.EntityType.ResolveIdentifier(Object value, ISessionImplementor session, Object owner)
  at NHibernate.Engine.TwoPhaseLoad.InitializeEntity(Object entity, Boolean readOnly, ISessionImplementor session, PreLoadEvent preLoadEvent, PostLoadEvent postLoadEvent)
  at NHibernate.Loader.Loader.InitializeEntitiesAndCollections(IList hydratedObjects, Object resultSetId, ISessionImplementor session, Boolean readOnly)
  at NHibernate.Loader.Loader.DoQuery(ISessionImplementor session, QueryParameters queryParameters, Boolean returnProxies)
  at NHibernate.Loader.Loader.DoQueryAndInitializeNonLazyCollections(ISessionImplementor session, QueryParameters queryParameters, Boolean returnProxies)
  at NHibernate.Loader.Loader.LoadCollection(ISessionImplementor session, Object id, IType type)
  at NHibernate.Loader.Collection.CollectionLoader.Initialize(Object id, ISessionImplementor session)
  at NHibernate.Persister.Collection.AbstractCollectionPersister.Initialize(Object key, ISessionImplementor session)
  at NHibernate.Event.Default.DefaultInitializeCollectionEventListener.OnInitializeCollection(InitializeCollectionEvent event)
  at NHibernate.Impl.SessionImpl.InitializeCollection(IPersistentCollection collection, Boolean writing)
  at NHibernate.Collection.AbstractPersistentCollection.Initialize(Boolean writing)
  at NHibernate.Collection.AbstractPersistentCollection.Read()
  at NHibernate.Collection.Generic.PersistentGenericBag`1.System.Collections.Generic.IEnumerable.GetEnumerator()
  at XXX.YYY.Services.FluentMdx.Queries.PrioMatrixQuery.SetupDimensionCollector(IDimensionCollector dimensionCollector)
  at lambda_method(Closure , ControllerBase , Object[] )
  at System.Web.Mvc.ReflectedActionDescriptor.Execute(ControllerContext controllerContext, IDictionary`2 parameters)
  at System.Web.Mvc.ControllerActionInvoker.InvokeActionMethod(ControllerContext controllerContext, ActionDescriptor actionDescriptor, IDictionary`2 parameters)
  at System.Web.Mvc.ControllerActionInvoker.&lt;&gt;c__DisplayClass15.b__12()
  at System.Web.Mvc.ControllerActionInvoker.InvokeActionMethodFilter(IActionFilter filter, ActionExecutingContext preContext, Func`1 continuation)
  at System.Web.Mvc.ControllerActionInvoker.InvokeActionMethodFilter(IActionFilter filter, ActionExecutingContext preContext, Func`1 continuation)
  at System.Web.Mvc.ControllerActionInvoker.InvokeActionMethodFilter(IActionFilter filter, ActionExecutingContext preContext, Func`1 continuation)
  at System.Web.Mvc.ControllerActionInvoker.InvokeActionMethodWithFilters(ControllerContext controllerContext, IList`1 filters, ActionDescriptor actionDescriptor, IDictionary`2 parameters)
  at System.Web.Mvc.ControllerActionInvoker.InvokeAction(ControllerContext controllerContext, String actionName) at NHibernate.Proxy.DefaultProxyFactory.GetProxy(Object id, ISessionImplementor session)
  at NHibernate.Tuple.Entity.AbstractEntityTuplizer.CreateProxy(Object id, ISessionImplementor session)
  at NHibernate.Persister.Entity.AbstractEntityPersister.CreateProxy(Object id, ISessionImplementor session)
  at NHibernate.Event.Default.DefaultLoadEventListener.CreateProxyIfNecessary(LoadEvent event, IEntityPersister persister, EntityKey keyToLoad, LoadType options, IPersistenceContext persistenceContext)
  at NHibernate.Event.Default.DefaultLoadEventListener.ProxyOrLoad(LoadEvent event, IEntityPersister persister, EntityKey keyToLoad, LoadType options)
  at NHibernate.Event.Default.DefaultLoadEventListener.OnLoad(LoadEvent event, LoadType loadType)
  at NHibernate.Impl.SessionImpl.FireLoad(LoadEvent event, LoadType loadType)
  at NHibernate.Impl.SessionImpl.InternalLoad(String entityName, Object id, Boolean eager, Boolean isNullable)
  at NHibernate.Type.EntityType.ResolveIdentifier(Object id, ISessionImplementor session)
  at NHibernate.Type.EntityType.ResolveIdentifier(Object value, ISessionImplementor session, Object owner)
  at NHibernate.Engine.TwoPhaseLoad.InitializeEntity(Object entity, Boolean readOnly, ISessionImplementor session, PreLoadEvent preLoadEvent, PostLoadEvent postLoadEvent)
  at NHibernate.Loader.Loader.InitializeEntitiesAndCollections(IList hydratedObjects, Object resultSetId, ISessionImplementor session, Boolean readOnly)
  at NHibernate.Loader.Loader.DoQuery(ISessionImplementor session, QueryParameters queryParameters, Boolean returnProxies)
  at NHibernate.Loader.Loader.DoQueryAndInitializeNonLazyCollections(ISessionImplementor session, QueryParameters queryParameters, Boolean returnProxies)
  at NHibernate.Loader.Loader.LoadCollection(ISessionImplementor session, Object id, IType type)
  at NHibernate.Loader.Collection.CollectionLoader.Initialize(Object id, ISessionImplementor session)
  at NHibernate.Persister.Collection.AbstractCollectionPersister.Initialize(Object key, ISessionImplementor session)
  at NHibernate.Event.Default.DefaultInitializeCollectionEventListener.OnInitializeCollection(InitializeCollectionEvent event)
  at NHibernate.Impl.SessionImpl.InitializeCollection(IPersistentCollection collection, Boolean writing)
  at NHibernate.Collection.AbstractPersistentCollection.Initialize(Boolean writing)
  at NHibernate.Collection.AbstractPersistentCollection.Read()
  at NHibernate.Collection.Generic.PersistentGenericBag`1.System.Collections.Generic.IEnumerable.GetEnumerator()
  at XXX.YYY.Services.FluentMdx.Queries.PrioMatrixQuery.SetupDimensionCollector(IDimensionCollector dimensionCollector)
  at XXX.YYY.Services.FluentMdx.Queries.QueryBuilder.b__1(IDimensionCollector x)
  at XXX.YYY.Services.FluentMdx.Queries.QueryBuilder.BuildQuery(IFluentMdxFilterCommand mdxFilterQueryCommand)
  at XXX.YYY.Services.FluentMdx.FluentMdxRepository.ExecuteQuery(IMdxQueryBuilder mdxQueryBuilder)
  at XXX.YYY.Services.Result.PrioMatrixRenderService.GetChartEngine(IReadOnlyQuerySettings querySettings, ChartSettingsModel settingsModel)
  at XXX.YYY.MVC.Controllers.PrioMatrixRenderController.RenderChart(QuerySettings queryable, ChartSettingsModel chartSettings)
  at lambda_method(Closure , ControllerBase , Object[] )
  at System.Web.Mvc.ReflectedActionDescriptor.Execute(ControllerContext controllerContext, IDictionary`2 parameters)
  at System.Web.Mvc.ControllerActionInvoker.InvokeActionMethod(ControllerContext controllerContext, ActionDescriptor actionDescriptor, IDictionary`2 parameters)
  at System.Web.Mvc.ControllerActionInvoker.&lt;&gt;c__DisplayClass15.b__12()
  at System.Web.Mvc.ControllerActionInvoker.InvokeActionMethodFilter(IActionFilter filter, ActionExecutingContext preContext, Func`1 continuation)
  at System.Web.Mvc.ControllerActionInvoker.InvokeActionMethodFilter(IActionFilter filter, ActionExecutingContext preContext, Func`1 continuation)
  at System.Web.Mvc.ControllerActionInvoker.InvokeActionMethodFilter(IActionFilter filter, ActionExecutingContext preContext, Func`1 continuation)
  at System.Web.Mvc.ControllerActionInvoker.InvokeActionMethodWithFilters(ControllerContext controllerContext, IList`1 filters, ActionDescriptor actionDescriptor, IDictionary`2 parameters)
  at System.Web.Mvc.ControllerActionInvoker.InvokeAction(ControllerContext controllerContext, String actionName)
  </code></pre>