Mar 22

NHibernate Proxy Generator

Tag: NHibernate, Open SourceSymon Rottem @ 4:11 pm

I just stumbled over an interesting post on William C. Pierce’s blog explaining how to set up NHibernate to run in a a medium trust environment and still get the benefits of lazy loading.

I’ve seen a few queries about how to do use lazy loading under medium trust in the past but have never really investigated it myself having assumed it was just one of those things you couldn’t do since proxies are generated through reflection and reflection permissions are not granted in medium trust environments.

Well it turns out that isn’t the case if you get a little creative.

What William has done is to create his own proxy factory that NHibernate can be configured to use so it will provide proxies that were generated at compile time where you have full trust rather than generating proxies at runtime where the reflection permissions are restricted.
He’s also provided a tool to generate those compile time proxies and released his hours of hard work as the NHibernate Proxy Generator (or NPG) so you don’t have to invest time of your own.  The project is still in it’s infancy but it’s released under the Apache License 2.0 so you can download the source and contribute should you so desire.

William has provided more information on how NHibernate can be configured to run in medium trust and use NPG generated proxies in his blog post so head on over there if you’re stuck with medium trust and still need lazy loading.

Related Posts
NHibernate ProxyGenerators added to NHContrib
Hammett goes to Microsoft
NHibernate 2.0 Alpha 2 Released
DDD and NHibernate
NHibernate 2.0 Beta 2 Released

3 Responses to “NHibernate Proxy Generator”

  1. Reflective Perspective - Chris Alcock » The Morning Brew #58 says:

    [...] NHibernate Proxy Generator – Some resources for getting NHibernate to play nicely in partial trust environments. [...]

  2. Symbiotic Development » NHibernate ProxyGenerators added to NHContrib says:

    [...] looks like the NHibernate Proxy Generator I discussed in a previous post has been added to the NHibernateForge, the NHibernate community and contribution [...]

  3. Mario says:

    Hi,

    I also had the problem making NHibernate work with medium trust and this is what I did:

    First..you need some sort of hosting that allows reflection in your server…There are some like crystaltech.com which will allow you to use reflection (in shared hosting) as long as it is used for assemblies within your designated app folder.

    Second…

    Download the Castle.DynamicProxy-vs2005 project. NHibernate ships with the dll, but you will need to modify the assembly to allow lazy loading under medium trust. Once you have the assembly, make sure nhibernate references this assembly and not the old dll.

    Open the Castle.DynamicProxy-vs2005 project and look for the ModuleScope.cs file (it is right in the main directory). There look for the method:
    private AssemblyName GetAssemblyName (bool signStrongName)
    In there comment out the following code:

    if (signStrongName)
    {
    byte[] keyPairStream = GetKeyPair();

    if (keyPairStream != null)
    {
    assemblyName.KeyPair = new StrongNameKeyPair(keyPairStream);
    }
    }

    Now give it a shot.

    Mario

Leave a Reply

Subscribe without commenting