Running NHibernate in Medium Trust
After deploying a NHibernate to a shared host I started running into a number of issues. Whilst developing we were running in full trust not in medium trust whilst the majority of shared hosts use. After hours and hours of searching I finally come across a post on the Castle Project mailing list which detailed how to get a nhibernate working under medium trust. Here is the steps I followed to get it working.
I am using NHibernate 2.1.2 and C# .NET 3.5 in Visual Studio 2008.
Step 1
Download Castle.Core from gitbhub
git://github.com/castleproject/Castle.Core.git
Step 2
Enable Castle to allow partially trusted callers.
Open buildscripts/CommonAssemblyInfo.cs and enable the library to enable partially trusted callers
Step 3
Disable generation of debug information for the projects Castle.Core and Castle.DynamicProxy.

Step 4
Build the projects
Step 5
Download Nhibernate source code from source forge.
http://sourceforge.net/projects/nhibernate/files/
Step 6
Open Nhibernate.Everything.sln and update the references for the NHibernate.ByteCode.Castle project so that the references for Castle.Core and Castle.DynamicProxy2 are the libraries build in step 4. Disable generation of debug information for the projects NHibernate, Iesi.Collections and NHibernate.ByteCode.Castle, similar to what was done in step 3.
Step 7
Copy the outputted library files from NHibernate.ByteCode/bin/Release to your shared libs folder of your project.
Step 8
Update the references in your project to point to the new libraries. Next turn off reflection optimization. This needs to be done in code before you create the configuration object. Configuring this in the hibernate.cfg.xml does not work. Update all your projects assemblies to set allow partially trusted callers, similar to step 2. Update your web.config to set requirePermission=”false” in the nhibernate section delceration
NHibernate.Cfg.Environment.UseReflectionOptimizer = false; Configuration = new Configuration(); Configuration.Configure();
You can now run your site in medium trust using lay loading.
Links
- msysgit – http://code.google.com/p/msysgit/
- TortoiseGit – http://code.google.com/p/tortoisegit/
- NHibernate – http://nhforge.org/
- Castle.Core Source – http://github.com/castleproject/
- NHibernate Source – http://sourceforge.net/projects/nhibernate/files/
Related Reading
![]() |
![]() |
![]() |














Can you post a full zip of your solution for both castle and NHibernate with the changes applied? I cannot seem to find the same version of castle.core that you used. I got a vs2008 and vs2010 version, but both have significant changes that do not work with nHibernate directly. Thank you!!