Pregunta

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.

¿Fue útil?

Solución

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.

Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top