Getting ActiveRecord and NHibernate Working in Medium Trust

I have spent the last couple of hours trying to figure out how to get Castle ActiveRecord working in medium trust (using GoDaddy), and I have finally succeeded (for my purposes).

Here are the steps I had to take to get this working:

 

* Re-built ActiveRecord to allow partially trusted callers, which entails the following steps: (thanks to this thread on the Castle Project forums)

1. Do an SVN Checkout on the Castle project trunk using TortoiseSVN or Subversion. The repository is here: http://svn.castleproject.org:8080/svn/castle/trunk

2. Navigate to the root directory of the Castle project on your machine and enter the following command:

SharedLibs\build\NAnt\bin\nant -t:net-3.5 -D:assembly.allow-partially-trusted-callers=true

Naturally, the build fails (running unit tests), but I was none-the-less able to get the updated DLLs I needed from the build\net-3.5\debug directory under the Castle directory.  I updated all the ActiveRecord-related DLLs (and NHibernate, etc.) that my app was using from the release version of ActiveRecord to this newly-compiled version.  It also now needs Iese.Collections.dll and NHibernate.ByteCode.Castle.dll

3. I added the following line to my ActiveRecord configuration file (appconfig.xml), which is required by newer versions of NHibernate (thanks to these instructions on NHForge):

<add key="proxyfactory.factory_class" value="NHibernate.ByteCode.Castle.ProxyFactoryFactory, NHibernate.ByteCode.Castle" />

* Enable AllowPartiallyTrustedCallers by adding the following code to AssemblyInfo.cs:

[assembly: System.Security.AllowPartiallyTrustedCallers]

 

* Place this line after your call to ActiveRecordStarter.Initialize(...): (thanks to these instructions on NHForge)
NHibernate.Cfg.Environment.UseReflectionOptimizer = false;

 

Apparently getting Lazy Loading to work in Medium Trust is even harder, but there is apparently a tool available called NHibernate Proxy Generator that will enable that to work, although it entails generating proxies at compile-time because Reflection is not allowed under Medium Trust.  Here is the tale of an individual who was apparently able to get this to work: http://blechie.com/WPierce/archive/2008/02/17/Lazy-Loading-with-nHibernate-Under-Medium-Trust.aspx  At present, I don't need lazy-loading in the application that I'm running in Medium Trust, so I haven't tried it.

 

Good luck!  Leave a comment if this worked for you, or if you have any problems.