NHibernate Forge
The official new home for the NHibernate community

Run in Medium Trust

Page Details

First published by:
Bill Pierce
on 09-22-2008
Last revision by:
Bill Pierce
on 09-26-2008
2 people found this article useful.
Article
Comments (1)
History (10)
100% of people found this useful

Run in Medium Trust

Filed under: [Edit Tags]

Using NHibernate in a Medium Trust web environment requires the following:

  • All referenced assembiles must be marked with AllowPartiallyTrustedCallers
  • Web.config configSections must be marked with requirePermission="false"
    <configSections>
     
    <section name="hibernate-configuration" requirePermission="false" type="NHibernate.Cfg.ConfigurationSectionHandler, NHibernate"/>
     
    <section name="log4net" requirePermission="false" type="log4net.Config.Log4NetConfigurationSectionHandler, log4net" />
     
    ...
    </configSections>
  • Reflection optimization must be disabled
    <hibernate-configuration xmlns="urn:nhibernate-configuration-2.2">
     
    <reflection-optimizer use="false" />
     
    ...
    </hibernate-configuration>
  • Default lazy loading must be disabled on all class mappings
    <hibernate-mapping xmlns="urn:nhibernate-mapping-2.2" default-lazy="false" assembly="Example.Domain" namespace="Example.Domain.Models">
     
    <class name="Person">
       
    ...
     
    </class>
    </
    hibernate-mapping>
    Alternativly you could keep lazy loading enabled (the default) and Pre-Generate Lazy Loading Proxies

 

Using ActiveRecord in a Medium Trust web environment requires the following:

  • All referenced assembiles must be marked with AllowPartiallyTrustedCallers
  • Web.config configSections must be marked with requirePermission="false"
    <configSections>
     
    <section name="activerecord" requirePermission="false" type="Castle.ActiveRecord.Framework.Config.ActiveRecordSectionHandler, Castle.ActiveRecord"/>
     
    <section name="log4net" requirePermission="false" type="log4net.Config.Log4NetConfigurationSectionHandler, log4net" />
     
    ...
    </configSections>
  • Reflection optimization must be disabled
    Place this line after your call to ActiveRecordStarter.Initialize(...):
    NHibernate.Cfg.Environment.UseReflectionOptimizer = false;

  • Default lazy loading must be disabled on all class mappings
    <activerecord isWeb="true" default-lazy="false">
     
    <config>
       
    ...
     
    </config>
    </
    activerecord>
    Alternativly you could keep lazy loading enabled (the default) and Pre-Generate Lazy Loading Proxies

Recent Comments

By: DavidCru Posted on 12-21-2008 9:36

I don't think that setting reflection-optimizer to false inside the hibernate-configuration will work. This is what I found in the nhibernate documentation:

"You can not set this property in hibernate.cfg.xml or <hibernate-configuration> section of the application configuration file."

Instead, you can set the reflection-optimizer to false in your code, before you load the configuration. Just write "NHibernate.Cfg.Environment.UseReflectionOptimizer = false;"

Another alternative is setting the parameter inside you web.config, in the nhibernate-settings. Just add

<nhibernate>

 <add key="hibernate.use_reflection_optimizer" value="false"/>

</nhibernate>

after configSections and also

<section name="nhibernate" type="System.Configuration.NameValueSectionHandler, System, Version=1.0.1.0,Culture=neutral, PublicKeyToken=b77a5c561934e089"/>

inside configSections.

These changes worked for me with my provider that only allows medium trust applications... Hopefully, this will also help others who encounter the same problems.

View All
Powered by Community Server (Commercial Edition), by Telligent Systems