Tuesday, 6 September 2011

Update NHibernate 3.2 and Medium Trust Environments

NOTE: This is an update from a previous post - Nhibernate 3.2 and Medium Trust

Another NOTE: NHibernate 3.3 does not have this problem. See this post for more info.
A few weeks ago I asked the community if somebody could help me figure out how to get NHibernate working in a Medium Trust environment. Well a person by the name of Randy Lee @ranzlee came to the rescue. Basically Randy downloaded the NHibernate source and got to work. He used PEVerify a tool on MSDN that:-
The PEVerify tool helps developers who generate Microsoft intermediate language (MSIL) (such as compiler writers, script engine developers, and so on) to determine whether their MSIL code and associated metadata meet type safety requirements. Some compilers generate verifiably type-safe code only if you avoid using certain language constructs. If, as a developer, you are using such a compiler, you may want to verify that you have not compromised the type safety of your code. In this situation, you can run the PEVerify tool on your files to check the MSIL and metadata.
Thankfully the results of PEVerify showed one error:
[IL]: Error: [c:\Users\Randy\generatedassembly.dll : CatProxy::.ctor][mdToken=0x6000001][offset 0x00000001][found ref ('this' ptr) 'CatProxy'] Call to .ctor only allowed to initialize this pointer from within a .ctor. Try newobj.
Randy figured out that he needed to fixed the proxy factory to emit call the "real" base type constructor, but this would have the side-effect of preventing constructor DI in entities. He then added a new provider and contract (DefaultEntityInjector and IEntityInjector) so that DI is supported out-of-box by providing an implementation. The implementation simply needs to provide the constructor arguments for NH's call to Activator.CreateInstance.

You can download Randy's version of NHibernate.DependencyInjection from Nuget.

Basically all you need to get up and running is to initialise the ByteCode Provider and you are up and running...
protected void Application_Start() {
  Initializer.RegisterBytecodeProvider();
  ...
  Rest of configuration code here
}
Also please note that if you think that NHibernate 3.2 should support Medium Trust environments out of the box then please visit the NHibernate Jira and vote the issue up, the more votes the more chance we have of getting this bug fixed.

Thanks

3 comments:

  1. mmmm... I've tried it, but on my provider I get this exception:

    [SecurityException: Request for the permission of type 'System.Security.Permissions.ReflectionPermission, mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089' failed.]
    NHibernate.DependencyInjection.BytecodeProvider..ctor() +40
    NHibernate.DependencyInjection.Initializer.RegisterBytecodeProvider() +40
    PB.Global.Application_Start(Object sender, EventArgs e) in Global.asax.cs:16

    ReplyDelete
  2. Hi, do you have the trust file at hand from your provider?

    ReplyDelete
  3. It happens in medium trust

    ReplyDelete