0

I am trying to get my web application written in ASP.NET 4.0 working locally using medium trust. I'm using Go Daddy as web host and they use medium trust on their shared hosts. I've read that nHibernate now works with Medium Trust sites out of the box in 3.3 but I cannot for the life of me get mine working.

By simply adding:

<trust level="Medium" />

to my web.config my web application bombs out with the following error and stack trace:

Request for the permission of type 'System.Security.Permissions.ReflectionPermission, mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089' failed.

at System.Security.CodeAccessSecurityEngine.Check(Object demand, StackCrawlMark& stackMark, Boolean isPermSet)
   at System.Security.CodeAccessSecurityEngine.Check(CodeAccessPermission cap, StackCrawlMark& stackMark)
   at System.Security.CodeAccessPermission.Demand()
   at System.Reflection.Emit.DynamicMethod.PerformSecurityCheck(Type owner, StackCrawlMark& stackMark, Boolean skipVisibility)
   at System.Reflection.Emit.DynamicMethod..ctor(String name, Type returnType, Type[] parameterTypes, Type owner, Boolean skipVisibility)
   at NHibernate.Bytecode.Lightweight.ReflectionOptimizer.CreateDynamicMethod(Type returnType, Type[] argumentTypes)
   at NHibernate.Bytecode.Lightweight.ReflectionOptimizer.GenerateGetPropertyValuesMethod(IGetter[] getters)
   at NHibernate.Bytecode.Lightweight.ReflectionOptimizer..ctor(Type mappedType, IGetter[] getters, ISetter[] setters)
   at NHibernate.Bytecode.Lightweight.BytecodeProviderImpl.GetReflectionOptimizer(Type mappedClass, IGetter[] getters, ISetter[] setters)
   at NHibernate.Tuple.Entity.PocoEntityTuplizer.SetReflectionOptimizer()
   at NHibernate.Tuple.Entity.PocoEntityTuplizer..ctor(EntityMetamodel entityMetamodel, PersistentClass mappedEntity)
   at NHibernate.Tuple.Entity.EntityEntityModeToTuplizerMapping..ctor(PersistentClass mappedEntity, EntityMetamodel em)
   at NHibernate.Tuple.Entity.EntityMetamodel..ctor(PersistentClass persistentClass, ISessionFactoryImplementor sessionFactory)
   at NHibernate.Persister.Entity.AbstractEntityPersister..ctor(PersistentClass persistentClass, ICacheConcurrencyStrategy cache, ISessionFactoryImplementor factory)
   at NHibernate.Persister.Entity.SingleTableEntityPersister..ctor(PersistentClass persistentClass, ICacheConcurrencyStrategy cache, ISessionFactoryImplementor factory, IMapping mapping)
   at NHibernate.Persister.PersisterFactory.CreateClassPersister(PersistentClass model, ICacheConcurrencyStrategy cache, ISessionFactoryImplementor factory, IMapping cfg)
   at NHibernate.Impl.SessionFactoryImpl..ctor(Configuration cfg, IMapping mapping, Settings settings, EventListeners listeners)
   at NHibernate.Cfg.Configuration.BuildSessionFactory()
   at NHibernate.DataAccess.WebTest.Code.NHibernateHelper.get_SessionFactory()
   at NHibernate.DataAccess.WebTest.Code.NHibernateHelper.OpenSession()
   at NHibernate.DataAccess.WebTest._Default.Page_Load(Object sender, EventArgs e)
   at System.Web.Util.CalliHelper.EventArgFunctionCaller(IntPtr fp, Object o, Object t, EventArgs e)
   at System.Web.Util.CalliEventHandlerDelegateProxy.Callback(Object sender, EventArgs e)
   at System.Web.UI.Control.OnLoad(EventArgs e)
   at System.Web.UI.Control.LoadRecursive()
   at System.Web.UI.Page.ProcessRequestMain(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint)

This seems to be the same error that I've reading about but none of the fixes I've found so far have seemed to work for me. I've even tried the NHibernate.DependencyInjection Nuget package but that errors out with the same error but a different stacktrace, which occurs in the Initializer ctor.

I feel like I'm missing something obvious to get this work but I can't put my finger on it. Has anyone had any successs running nHibernate 3.3.1 in medium trust?

2 Answers2

2

I was able to get this working by turning off ado batching. I found the answer here, http://blog.ondrejsv.com/post/N2CMS-NHibernate-SqlCommandSet-and-medium-trust.aspx. Warning, I haven't migrated this to production yet; this is just on my laptop (YMMV).

<property name="adonet.batch_size">0</property>

I tried NHibernate.DependencyInjection package and it didn't work.

Hope this helps.

Wibber
  • 41
  • 2
1

I use NH 3.3.1 out of the box with my medium trust provider. However there may be something else to help you out. Install this package by Ranzlee:-

PM> Install-Package NHibernate.DependencyInjection

To get it up and running just use

protected void Application_Start() {
  Initializer.RegisterBytecodeProvider();
  ...
  Rest of configuration code here
}
Rippo
  • 22,117
  • 14
  • 78
  • 117