Saturday, 27 March 2010

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