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 Web Hosting at 12/22/2009 8:27 AM

I really appreciate your concise instructions, but I'm running into a problem: my project depends on libraries such as Castle.Monorail.Framework.
Gravatar # re: Getting ActiveRecord and NHibernate Working in Medium Trust
by insurance at 1/14/2010 8:32 AM

Nobody really talks about the real-world scenarios. It's easy to understand the mapping and the blog idea in a read-only scenario, but what happens when you change the Foreign Key inside the aggregated table and want to update and save the main record, for example?

Say, for instance, we have:

Patient => Case <= Doctor

Supposing we entered a wrong Patient ID in the case table and we want to fix, what happens when you change the foreign id in the Case table and try to save the record?
Gravatar # re: Getting ActiveRecord and NHibernate Working in Medium Trust
by alternative casinos at 1/20/2010 4:37 AM

I believe in God so god is control and what happens here is only the smallest glimpse of something far greater that He has in mind for us.
Gravatar # re: Getting ActiveRecord and NHibernate Working in Medium Trust
by alternative casinos at 1/20/2010 4:37 AM

I believe in God so god is control and what happens here is only the smallest glimpse of something far greater that He has in mind for us.
Gravatar # re: Getting ActiveRecord and NHibernate Working in Medium Trust
by ppo plans at 2/1/2010 10:08 AM

One thing I knew I needed to do was to test against a true database instance, not a mock. I wanted to make sure this was a live test of the persistence model, not just a test of the conceptual model. Since CSFBL will only run off an MSSQL database, and the schema is constantly changing, and the domain model has many foreign key dependencies, unit testing against the database is important.
Gravatar # re: Getting ActiveRecord and NHibernate Working in Medium Trust
by Free Dofollow sites List at 2/3/2010 6:33 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.
Gravatar # re: Getting ActiveRecord and NHibernate Working in Medium Trust
by Love poetry at 2/5/2010 5:29 AM

Supposing we entered a wrong Patient ID in the case table and we want to fix, what happens when you change the foreign id in the Case table and try to save the record?
Gravatar # re: Getting ActiveRecord and NHibernate Working in Medium Trust
by MonroeCandice20 at 3/10/2010 1:41 AM

A reliable thesis service is a light in the tunnel, just because it can accomplish the sample thesis of high quality.

Your comment:

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