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.

Print | posted @ Tuesday, June 23, 2009 10:31 PM

Comments on this entry:

Gravatar # re: Getting ActiveRecord and NHibernate Working in Medium Trust
by Brian Kendig at 10/30/2009 10:42 AM

I really appreciate your concise instructions, but I'm running into a problem: my project depends on libraries such as Castle.Monorail.Framework.dll and Castle.MonoRail.Views.NVelocity.dll, but these don't get created in the build/net-3.5/debug directory (or anywhere else I've found), and when I try to run my project it fails with a "Could not load file or assembly 'Castle.Core, Version=1.0.3.0'" error. The Castle.Core.dll that's generated by your process ends up with version 1.1.0.0 on it.

Any help would be appreciated, thank you!

Gravatar # re: Getting ActiveRecord and NHibernate Working in Medium Trust
by teresa at 6/3/2010 3:08 AM

Hi! I just cant checkout http://svn.castleproject.org:8080/svn/castle/trunk
at all. The castle site says that the source code has moved to the GIT respository from 2010.
What can the issue be???
Thanks

Your comment:

Title:
Name:
Email:
Website:
 
Italic Underline Blockquote Hyperlink
 
 
Please add 6 and 5 and type the answer here: