-->
These old forums are deprecated now and set to read-only. We are waiting for you on our new forums!
More modern, Discourse-based and with GitHub/Google/Twitter authentication built-in.

All times are UTC - 5 hours [ DST ]



Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 12 posts ] 
Author Message
 Post subject: Medium Trust still not working
PostPosted: Sun Jan 21, 2007 2:30 pm 
Newbie

Joined: Sun Jan 21, 2007 2:26 pm
Posts: 4
Hibernate version: 1.2.0beta 3

Thought the Jira says that the medium trust level issue is fixed in 1.2.0 beta 1, I am still having the problem in beta 3.

Running on a hosted medium trust server I get the following exception. Any ideas?

Code:
Security Exception
Description: The application attempted to perform an operation not allowed by the security policy.  To grant this application the required permission please contact your system administrator or change the application's trust level in the configuration file.

Exception Details: System.Security.SecurityException: Request for the permission of type 'System.Security.Permissions.ReflectionPermission, mscorlib, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089' failed.

Source Error:


Line 27:             cfg.AddXmlFile(basePath + "Species.hbm.xml");
Line 28:
Line 29:             sessionFactory = cfg.BuildSessionFactory();
Line 30:         }
Line 31:


Source File: f:\hshome\p5inc\foo.bar.com\App_Code\DatabaseHelper.cs    Line: 29

Stack Trace:


[SecurityException: Request for the permission of type 'System.Security.Permissions.ReflectionPermission, mscorlib, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089' failed.]
   System.Security.CodeAccessSecurityEngine.Check(Object demand, StackCrawlMark& stackMark, Boolean isPermSet) +0
   System.Security.CodeAccessPermission.Demand() +59
   System.Reflection.Emit.DynamicMethod.PerformSecurityCheck(Type owner, StackCrawlMark& stackMark, Boolean skipVisibility) +213
   System.Reflection.Emit.DynamicMethod..ctor(String name, Type returnType, Type[] parameterTypes, Type owner, Boolean skipVisibility) +47
   NHibernate.Bytecode.Lightweight.ReflectionOptimizer.CreateDynamicMethod(Type returnType, Type[] argumentTypes) in ReflectionOptimizer.cs:101
   NHibernate.Bytecode.Lightweight.ReflectionOptimizer.GenerateGetPropertyValuesMethod(IGetter[] getters) in ReflectionOptimizer.cs:122
   NHibernate.Bytecode.Lightweight.ReflectionOptimizer..ctor(Type mappedType, IGetter[] getters, ISetter[] setters) in ReflectionOptimizer.cs:47
   NHibernate.Bytecode.Lightweight.BytecodeProviderImpl.GetReflectionOptimizer(Type mappedClass, IGetter[] getters, ISetter[] setters) in BytecodeProviderImpl.cs:33
   NHibernate.Persister.Entity.AbstractEntityPersister..ctor(PersistentClass persistentClass, ICacheConcurrencyStrategy cache, ISessionFactoryImplementor factory) in AbstractEntityPersister.cs:852
   NHibernate.Persister.Entity.SingleTableEntityPersister..ctor(PersistentClass model, ICacheConcurrencyStrategy cache, ISessionFactoryImplementor factory, IMapping mapping) in SingleTableEntityPersister.cs:265
   NHibernate.Persister.PersisterFactory.CreateClassPersister(PersistentClass model, ICacheConcurrencyStrategy cache, ISessionFactoryImplementor factory, IMapping cfg) in PersisterFactory.cs:50
   NHibernate.Impl.SessionFactoryImpl..ctor(Configuration cfg, IMapping mapping, Settings settings) in SessionFactoryImpl.cs:188
   NHibernate.Cfg.Configuration.BuildSessionFactory() in Configuration.cs:1063
   Database.DatabaseHelper..cctor() in f:\hshome\p5inc\foo.bar.com\App_Code\DatabaseHelper.cs:29
[/code]


Top
 Profile  
 
 Post subject:
PostPosted: Mon Jan 22, 2007 4:09 am 
Contributor
Contributor

Joined: Wed May 11, 2005 4:59 pm
Posts: 1766
Location: Prague, Czech Republic
Check that you are only using public properties. Under medium trust, no code can access private members of a class through reflection, and NHibernate is no exception.

If it doesn't help, disable the reflection optimizer (set hibernate.use_reflection_optimizer to false).


Top
 Profile  
 
 Post subject:
PostPosted: Mon Jan 22, 2007 10:43 am 
Newbie

Joined: Sun Jan 21, 2007 2:26 pm
Posts: 4
Thanks, I'll give the optimizer a try. In this case, all my properties are public, though my member variables are not (NHibernate doesn't allow them to be).


Top
 Profile  
 
 Post subject:
PostPosted: Mon Jan 22, 2007 11:09 am 
Newbie

Joined: Sun Jan 21, 2007 2:26 pm
Posts: 4
Turning off the reflection optimizer did indeed solve the reflection issue. Only to be followed by this one. Again, I welcome any suggestions. If I can get this all working, I plan to blog the fixes so everyone can do it :-)

Code:
Server Error in '/' Application.
--------------------------------------------------------------------------------

Security Exception
Description: The application attempted to perform an operation not allowed by the security policy.  To grant this application the required permission please contact your system administrator or change the application's trust level in the configuration file.

Exception Details: System.Security.SecurityException: Request for the permission of type 'System.Security.Permissions.FileIOPermission, mscorlib, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089' failed.

Source Error:

An unhandled exception was generated during the execution of the current web request. Information regarding the origin and location of the exception can be identified using the exception stack trace below. 

Stack Trace:


[SecurityException: Request for the permission of type 'System.Security.Permissions.FileIOPermission, mscorlib, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089' failed.]
   System.Security.CodeAccessSecurityEngine.Check(Object demand, StackCrawlMark& stackMark, Boolean isPermSet) +0
   System.Security.CodeAccessPermission.Demand() +59
   System.Reflection.Assembly.VerifyCodeBaseDiscovery(String codeBase) +118
   System.Reflection.Assembly.GetName(Boolean copiedName) +59
   System.Reflection.Assembly.GetName() +6
   Castle.DynamicProxy.Builder.CodeBuilder.EasyType.IsAssemblySigned(Type baseType) +193
   Castle.DynamicProxy.Builder.CodeBuilder.EasyType..ctor(ModuleScope modulescope, String name, Type baseType, Type[] interfaces, Boolean serializable) +94
   Castle.DynamicProxy.Builder.CodeGenerators.BaseCodeGenerator.CreateTypeBuilder(String typeName, Type baseType, Type[] interfaces) +88
   Castle.DynamicProxy.Builder.CodeGenerators.ClassProxyGenerator.GenerateCode(Type baseClass, Type[] interfaces) +359
   Castle.DynamicProxy.Builder.DefaultProxyBuilder.CreateClassProxy(Type theClass, Type[] interfaces) +74
   Castle.DynamicProxy.ProxyGenerator.CreateClassProxy(Type baseClass, Type[] interfaces, IInterceptor interceptor, Boolean checkAbstract, Object[] argumentsForConstructor) +109
   NHibernate.Proxy.CastleProxyFactory.GetProxy(Object id, ISessionImplementor session) in CastleProxyFactory.cs:58





Top
 Profile  
 
 Post subject:
PostPosted: Mon Jan 22, 2007 11:33 am 
Contributor
Contributor

Joined: Wed May 11, 2005 4:59 pm
Posts: 1766
Location: Prague, Czech Republic
You'll have to disable proxies (set lazy="false" for all classes). DynamicProxy does not work under medium trust as far as I can tell, and there isn't anything I can do about it, unfortunately.


Top
 Profile  
 
 Post subject:
PostPosted: Mon Jan 22, 2007 11:45 am 
Newbie

Joined: Sun Jan 21, 2007 2:26 pm
Posts: 4
That seems to have worked. I've been muddling my way through the Castle forum and it seems that they're working on their own fix, so I'm hopeful that NHibernate will "just work" in medium trust soon.

So to sum it up:

To make NHibernate 1.2 work in medium trust, you must:

1) Make sure all class properties are public to avoid reflection issues.
2) Turn off the reflection optimizer:
Code:
<nhibernate>
  <add key="hibernate.use_reflection_optimizer" value="False" />
</nhibernate>

3) Disable lazy proxies by setting lazy="false" for all class mappings.

Thanks very much! [/code]


Top
 Profile  
 
 Post subject:
PostPosted: Tue Mar 13, 2007 4:22 am 
Newbie

Joined: Tue Jul 18, 2006 9:06 am
Posts: 15
Location: .NET
gpasq wrote:
That seems to have worked. I've been muddling my way through the Castle forum and it seems that they're working on their own fix, so I'm hopeful that NHibernate will "just work" in medium trust soon.

So to sum it up:

To make NHibernate 1.2 work in medium trust, you must:

1) Make sure all class properties are public to avoid reflection issues.
2) Turn off the reflection optimizer:
Code:
<nhibernate>
  <add key="hibernate.use_reflection_optimizer" value="False" />
</nhibernate>

3) Disable lazy proxies by setting lazy="false" for all class mappings.

Thanks very much! [/code]


Hi!

I still have the problem on my site: http://www.jobpost.co.il/Search.aspx
I checked all my classes are public, lazy=false and I added
<property name="hibernate.use_reflection_optimizer">false</property>
in my web.config where all parameters of NHibernate. Is it right?
Do you have any suggestions?

_________________
"A successful person is one who can lay a firm foundation with the bricks that others throw at him."
http://www.russiangeek.com


Top
 Profile  
 
 Post subject:
PostPosted: Tue Mar 13, 2007 4:35 am 
Newbie

Joined: Tue Jul 18, 2006 9:06 am
Posts: 15
Location: .NET
gpasq wrote:
2) Turn off the reflection optimizer:
Code:
<nhibernate>
  <add key="hibernate.use_reflection_optimizer" value="False" />
</nhibernate> 

I also see in documentation:
You can not set this property in hibernate.cfg.xml or <hibernate-configuration> section of the application configuration file.
So where can I set it?

_________________
"A successful person is one who can lay a firm foundation with the bricks that others throw at him."
http://www.russiangeek.com


Top
 Profile  
 
 Post subject:
PostPosted: Fri Apr 06, 2007 2:22 pm 
Newbie

Joined: Fri Apr 06, 2007 2:17 pm
Posts: 2
I was able to set the hibernate.use_reflection_optimizer property in the web.config file as follows. Note that the setting did not work within the hibernate-configuration section, so I had to place it in a new nhibernate section. The code now appears to be working in a medium trust environment ( godaddy )

Code:
  <configSections>
    <section name="hibernate-configuration"
             type="NHibernate.Cfg.ConfigurationSectionHandler, NHibernate"
             requirePermission="false"/>
    <section name="nhibernate"
             type="System.Configuration.NameValueSectionHandler, System, Version=1.0.5000.0,Culture=neutral, PublicKeyToken=b77a5c561934e089"
             requirePermission="false"/>
  </configSections>

  <hibernate-configuration xmlns="urn:nhibernate-configuration-2.2">
    <session-factory>
      <property name="connection.provider">NHibernate.Connection.DriverConnectionProvider</property>
      <property name="dialect">NHibernate.Dialect.MySQLDialect</property>
      <property name="connection.driver_class">NHibernate.Driver.MySqlDataDriver</property>
      <property name="connection.connection_string">blahblah</property>
      </session-factory>
  </hibernate-configuration>

  <nhibernate>
    <add key="hibernate.use_reflection_optimizer" value="false" />
  </nhibernate>


_________________
languagegame


Top
 Profile  
 
 Post subject: still having problems
PostPosted: Mon Apr 16, 2007 7:02 am 
Newbie

Joined: Thu Oct 05, 2006 10:08 am
Posts: 3
Hi there
I've followed the instructions above and although I've got past the ReflectionPermission exception I now get the following:

Security Exception
Description: ...

Exception Details: System.Security.SecurityException: That assembly does not allow partially trusted callers.

Source Error:
...

Stack Trace:

[SecurityException: That assembly does not allow partially trusted callers.]
NHibernate.Proxy.CastleProxyFactory..cctor() +0

Looks like it's the DynamicProxy component - I've read about medium trust problems with this in other forums. However I assumed that the NHibernate configuration setting regarding the reflection optimiser (turning it off) would stop NHibernate from using the DynamicProxy component. Is this wrong?

I have also set all of my classes to Lazy="false" in my mapping files. My properties are all public however these expose member variables which are private. Should I make *all* variables in my classes public? If so it would make more sense to do away with properties and define all members as public virtual member variables.

i.e:

public virtual int ID;

as opposed to:
private int mID;
public virtual ID { get { return mID; } set { mID = value; } }

The ultimate question is - is there a way to use NHibernate in a medium trust environment without being granted ReflectionPermission?


Top
 Profile  
 
 Post subject:
PostPosted: Mon Apr 16, 2007 7:08 am 
Contributor
Contributor

Joined: Wed May 11, 2005 4:59 pm
Posts: 1766
Location: Prague, Czech Republic
Disabling proxies, i.e. setting all classes to lazy="false" should work. You probably forgot to disable proxies for a class.

There is no such thing as a virtual member variable.

Public members only need to be virtual if proxying is enabled for a class, you can leave them non-virtual otherwise. In particular, if you are getting an error about something not being virtual, it means that the relevant class is set up as lazy.


Top
 Profile  
 
 Post subject:
PostPosted: Mon Apr 16, 2007 7:10 am 
Newbie

Joined: Thu Oct 05, 2006 10:08 am
Posts: 3
Aha - thanks.

I didn't realise you couldn't make a member variable virtual but it makes perfect sense now that I think about it.

I'll have a better look at my code.

Cheers


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 12 posts ] 

All times are UTC - 5 hours [ DST ]


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
© Copyright 2014, Red Hat Inc. All rights reserved. JBoss and Hibernate are registered trademarks and servicemarks of Red Hat, Inc.