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














10 comments:

  1. [...] This post was mentioned on Twitter by Brendan Kowitz. Brendan Kowitz said: woohoo! running nhibernate / castle dynamic proxy in medium trust works! thanks to this post: http://bit.ly/aMEgYv [...]

    ReplyDelete
  2. 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!!

    ReplyDelete
  3. Here is a version that I compiled to work view .NET 2.0

    http://blog.yeticode.co.uk/wp-content/plugins/download-monitor/download.php?id=4

    ReplyDelete
  4. Hey John,

    Having the same issue as Curtis and that download link appears to not be working. I'd be awesome if you could put them in another location. All I've been able to find are the 2010 files. Thanks!

    ReplyDelete
  5. They should be accessible from here, the plugin was using to sort out downloads broke.

    http://blog.yeticode.co.uk/wp-content/uploads/downloads/2010/06/libs.zip

    ReplyDelete
  6. John on behalf of everyone out there who read your post and DID NOT want to do all of that.....thank you, thank you, thank you and THANK YOU!!!

    ReplyDelete
  7. Thanks. I don't get the last part of it.. where should I put the Nhibernate.Cfg...... and the web.config section where should it go? I really dont got anything in my web.config for my nhibernate. I use fluent nhibernate and dont got no xml files at all its all classes..

    ReplyDelete
  8. I tried using the library provided, but still issue persists

    have this in web.config

    and following in hibernate.cfg.xml....


    Exception Details: System.Security.SecurityException: Request for the permission of type 'System.Security.Permissions.ReflectionPermission, mscorlib, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089' failed.

    ReplyDelete
  9. tags made content not to appear....

    hibernate.cfg.xml ........... have ....... reflection-optimizer use="false"
    web.config ............ section name="hibernate-configuration" type="NHibernate.Cfg.ConfigurationSectionHandler, NHibernate" requirePermission="false"

    ReplyDelete