2

A friend made a ASP.NET website work with NHibernate locally on his computer, and on my computer after I downloaded it from a SVN repository. However, we're trying to host it on a shared hosting environment with Medium Trust Level. The server gives the following exception (partial stacktrace):

[SecurityException: Request for the permission of type 'System.Security.Permissions.ReflectionPermission, mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089' failed.]
   System.Security.CodeAccessSecurityEngine.Check(Object demand, StackCrawlMark& stackMark, Boolean isPermSet) +0
   System.Security.CodeAccessSecurityEngine.Check(CodeAccessPermission cap, StackCrawlMark& stackMark) +31
   System.Security.CodeAccessPermission.Demand() +46
   System.Reflection.Emit.DynamicMethod.PerformSecurityCheck(Type owner, StackCrawlMark& stackMark, Boolean skipVisibility) +166
   System.Reflection.Emit.DynamicMethod..ctor(String name, Type returnType, Type[] parameterTypes, Type owner, Boolean skipVisibility) +40
   NHibernate.Bytecode.Lightweight.ReflectionOptimizer.CreateDynamicMethod(Type returnType, Type[] argumentTypes) in ReflectionOptimizer.cs:104

Something to do with reflection. On NHforge.org is a guide that tells to switch off the Reflection Optimiser. I can't find how to do it but I suspect you simply can't do it at all in an ASP.NET Website in VS2010. Someone else on SO suggested to convert it to an ASP.NET Web application first, where you have more control on the assembly references.

The problem is that the deadline for the website (it's a school project) is already friday of next week. We haven't learnt any kind of ORM but we were hoping it could save us a lot of time. I would like to pose 3 questions:

  1. Given the exception and the fact that it's a shared hosting with medium trust, will we possibly get NHibernate working in a decent amount of time?

  2. How do we convert our web site to a web application?

  3. What further steps do we need to take?

I know it's not really allowed to ask more than one question at a time but these questions are very related and I have a feeling that there is a single answer to all of them.

Any help is appreciated. If you need the full stacktrace I'll post it.

MarioDS
  • 12,895
  • 15
  • 65
  • 121

1 Answers1

1

See this blog post and also this one. I needed to use NH3.2 on Rackspace cloud which uses Medium Trust. All you need to do is run this Nuget command and it should download the correct assemblies (you may need to remove your existing NH dll's first).

PM> Install-Package NHibernate.DependencyInjection

The nuget packagage that can be found here.

Then put this code in your app:-

protected void Application_Start() {  
  Initializer.RegisterBytecodeProvider();  
  ...  
  Rest of configuration code here  
}  

It looks like Randy has updated this to work with NH3.3.

Rippo
  • 22,117
  • 14
  • 78
  • 117
  • I'll let you know the result when my friend has time to work on it. He's been trying all day yesterday for it to work and I don't really understand any of it because I have no experience. He knows a bit of Java hibernate so he's got background knowledge. – MarioDS May 02 '12 at 17:55
  • Well he said he tried, but to no avail... thanks for your help anyway. – MarioDS May 03 '12 at 12:06
  • Is he using Linq? If so still install the dependency injection module and try using `session.QueryOver` instead. – Rippo May 03 '12 at 12:23
  • Another idea is to see if your hosting provider can provide their medium trust file for you to test against. You can then drop into your solution – Rippo May 03 '12 at 12:40
  • We have tested with medium trust locally just by setting that in the web config. Then it doesn't even work. I really appreciate that you want to help but I'm afraid we're going to have to drop NHibernate on this one and just do everyting manually... It's only a small project. – MarioDS May 03 '12 at 12:49