Assembly does not allow partially trusted callers

Topics: Exception Handling Application Block, Logging Application Block
Nov 5, 2007 at 4:33 AM
I have created Exception Handling Policy that shows a custom error message using Custom Handler and logs it to event log using Logging Handler. I am using this policy in various layers of my solution. Issue is when I call HandleException method in a project of type class library, it gives the following error. If I call the same method in a Windows Form project it works alright. What is going wrong here, why does the same code works in Win Form app and not Class Library? Please help

===================================

That assembly does not allow partially trusted callers. (Microsoft.Practices.EnterpriseLibrary.ExceptionHandling.Logging)

------------------------------
Program Location:

at System.Security.CodeAccessSecurityEngine.ThrowSecurityException(Assembly asm, PermissionSet granted, PermissionSet refused, RuntimeMethodHandle rmh, SecurityAction action, Object demand, IPermission permThatFailed)
at Microsoft.Practices.EnterpriseLibrary.ExceptionHandling.Logging.Configuration.LoggingExceptionHandlerAssembler.Assemble(IBuilderContext context, ExceptionHandlerData objectConfiguration, IConfigurationSource configurationSource, ConfigurationReflectionCache reflectionCache)
at Microsoft.Practices.EnterpriseLibrary.Common.Configuration.ObjectBuilder.AssemblerBasedObjectFactory`2.Create(IBuilderContext context, TConfiguration objectConfiguration, IConfigurationSource configurationSource, ConfigurationReflectionCache reflectionCache)
at Microsoft.Practices.EnterpriseLibrary.ExceptionHandling.ExceptionPolicyEntryCustomFactory.Create(IBuilderContext context, ExceptionTypeData objectConfiguration, IConfigurationSource configurationSource, ConfigurationReflectionCache reflectionCache)
at Microsoft.Practices.EnterpriseLibrary.ExceptionHandling.ExceptionPolicyCustomFactory.CreateObject(IBuilderContext context, String name, IConfigurationSource configurationSource, ConfigurationReflectionCache reflectionCache)
at Microsoft.Practices.EnterpriseLibrary.Common.Configuration.ObjectBuilder.ConfiguredObjectStrategy.BuildUp(IBuilderContext context, Type t, Object existing, String id)
at Microsoft.Practices.ObjectBuilder.BuilderStrategy.BuildUp(IBuilderContext context, Type typeToBuild, Object existing, String idToBuild)
at Microsoft.Practices.ObjectBuilder.SingletonStrategy.BuildUp(IBuilderContext context, Type typeToBuild, Object existing, String idToBuild)
at Microsoft.Practices.ObjectBuilder.BuilderStrategy.BuildUp(IBuilderContext context, Type typeToBuild, Object existing, String idToBuild)
at Microsoft.Practices.EnterpriseLibrary.Common.Configuration.ObjectBuilder.ConfigurationNameMappingStrategy.BuildUp(IBuilderContext context, Type t, Object existing, String id)
at Microsoft.Practices.ObjectBuilder.BuilderBase`1.DoBuildUp(IReadWriteLocator locator, Type typeToBuild, String idToBuild, Object existing, PolicyList[] transientPolicies)
at Microsoft.Practices.ObjectBuilder.BuilderBase`1.BuildUp(IReadWriteLocator locator, Type typeToBuild, String idToBuild, Object existing, PolicyList[] transientPolicies)
at Microsoft.Practices.ObjectBuilder.BuilderBase`1.BuildUpTTypeToBuild(IReadWriteLocator locator, String idToBuild, Object existing, PolicyList[] transientPolicies)
at Microsoft.Practices.EnterpriseLibrary.Common.Configuration.ObjectBuilder.EnterpriseLibraryFactory.BuildUpT(IReadWriteLocator locator, String id, IConfigurationSource configurationSource)
at Microsoft.Practices.EnterpriseLibrary.Common.Configuration.ObjectBuilder.LocatorNameTypeFactoryBase`1.Create(String name)
at Trinity.ExceptionHandling.ExceptionPolicy.HandleException(Exception exception, String policyName)

Nov 5, 2007 at 11:18 AM
Hi,

Unless you do something explicit about it (like using the .net config console to specify custom security policies), class libraries and the winforms application that uses them should have the same trust settings; there has to be more to your scenario than the winform app/class library nature of the caller.

My guess is you're using the signed EntLib binaries with a web app running in medium trust, but this detail doesn't really matter. These binaries do not have the APTCA (see http://blogs.msdn.com/shawnfa/archive/2005/02/04/367390.aspx for details), so they cannot be used for partially trusted applications. You can use EntLib for these apps if you build your own, unsigned version of the library. This is unfortunately not as simple as it should, as the ObjectBuilder assembly used by EntLib is shipped as a binary; this thread http://www.codeplex.com/entlib/Thread/View.aspx?ThreadId=10710 discusses how to perform this task.

Hope this helps,
Fernando

Nov 5, 2007 at 12:55 PM
Edited Nov 5, 2007 at 10:16 PM
hi fsimonazzi

after doing the steps in the above mentione link I get the followin error

===================================

An error occurred loading a configuration file: Request for the permission of type 'System.Security.Permissions.FileIOPermission, mscorlib, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089' failed. (C:\Documents and Settings\dkerry\Local Settings\Application Data\assembly\dl3\NJ5TCO7T.77O\55R92G3X.PXH\6ffabc3a\d0501f65_b21fc801\Austock.OfficeSystem.General.DLL.config) (System.Configuration)

------------------------------
Program Location:

at System.Configuration.BaseConfigurationRecord.EvaluateOne(String[] keys, SectionInput input, Boolean isTrusted, FactoryRecord factoryRecord, SectionRecord sectionRecord, Object parentResult)
at System.Configuration.BaseConfigurationRecord.Evaluate(FactoryRecord factoryRecord, SectionRecord sectionRecord, Object parentResult, Boolean getLkg, Boolean getRuntimeObject, Object& result, Object& resultRuntimeObject)
at System.Configuration.BaseConfigurationRecord.GetSectionRecursive(String configKey, Boolean getLkg, Boolean checkPermission, Boolean getRuntimeObject, Boolean requestIsHere, Object& result, Object& resultRuntimeObject)
at System.Configuration.BaseConfigurationRecord.GetSection(String configKey, Boolean getLkg, Boolean checkPermission)
at System.Configuration.BaseConfigurationRecord.GetSection(String configKey)
at System.Configuration.Configuration.GetSection(String sectionName)
at Microsoft.Practices.EnterpriseLibrary.Common.Configuration.FileConfigurationSourceImplementation.GetSection(String sectionName)
at Microsoft.Practices.EnterpriseLibrary.Common.Configuration.FileConfigurationSource.GetSection(String sectionName)
at Microsoft.Practices.EnterpriseLibrary.Logging.Configuration.LoggingSettings.GetLoggingSettings(IConfigurationSource configurationSource)
at Microsoft.Practices.EnterpriseLibrary.Logging.LogWriterStructureHolderCustomFactory.CreateObject(IBuilderContext context, String name, IConfigurationSource configurationSource, ConfigurationReflectionCache reflectionCache)
at Microsoft.Practices.EnterpriseLibrary.Logging.LogWriterCustomFactory.CreateObject(IBuilderContext context, String name, IConfigurationSource configurationSource, ConfigurationReflectionCache reflectionCache)
at Microsoft.Practices.EnterpriseLibrary.Common.Configuration.ObjectBuilder.ConfiguredObjectStrategy.BuildUp(IBuilderContext context, Type t, Object existing, String id)
at Microsoft.Practices.ObjectBuilder.BuilderStrategy.BuildUp(IBuilderContext context, Type typeToBuild, Object existing, String idToBuild) in C:\EntLib3Src\ObjectBuilder-1.0.51206.0-source\ObjectBuilder\BuilderStrategy.cs:line 39
at Microsoft.Practices.ObjectBuilder.SingletonStrategy.BuildUp(IBuilderContext context, Type typeToBuild, Object existing, String idToBuild) in C:\EntLib3Src\ObjectBuilder-1.0.51206.0-source\ObjectBuilder\Strategies\Singleton\SingletonStrategy.cs:line 48
at Microsoft.Practices.ObjectBuilder.BuilderStrategy.BuildUp(IBuilderContext context, Type typeToBuild, Object existing, String idToBuild) in C:\EntLib3Src\ObjectBuilder-1.0.51206.0-source\ObjectBuilder\BuilderStrategy.cs:line 39
at Microsoft.Practices.EnterpriseLibrary.Common.Configuration.ObjectBuilder.ConfigurationNameMappingStrategy.BuildUp(IBuilderContext context, Type t, Object existing, String id)
at Microsoft.Practices.EnterpriseLibrary.ExceptionHandling.Logging.Configuration.LoggingExceptionHandlerAssembler.Assemble(IBuilderContext context, ExceptionHandlerData objectConfiguration, IConfigurationSource configurationSource, ConfigurationReflectionCache reflectionCache)
at Microsoft.Practices.EnterpriseLibrary.Common.Configuration.ObjectBuilder.AssemblerBasedObjectFactory`2.Create(IBuilderContext context, TConfiguration objectConfiguration, IConfigurationSource configurationSource, ConfigurationReflectionCache reflectionCache)
at Microsoft.Practices.EnterpriseLibrary.ExceptionHandling.ExceptionPolicyEntryCustomFactory.Create(IBuilderContext context, ExceptionTypeData objectConfiguration, IConfigurationSource configurationSource, ConfigurationReflectionCache reflectionCache)
at Microsoft.Practices.EnterpriseLibrary.ExceptionHandling.ExceptionPolicyCustomFactory.CreateObject(IBuilderContext context, String name, IConfigurationSource configurationSource, ConfigurationReflectionCache reflectionCache)
at Microsoft.Practices.EnterpriseLibrary.Common.Configuration.ObjectBuilder.ConfiguredObjectStrategy.BuildUp(IBuilderContext context, Type t, Object existing, String id)
at Microsoft.Practices.ObjectBuilder.BuilderStrategy.BuildUp(IBuilderContext context, Type typeToBuild, Object existing, String idToBuild) in C:\EntLib3Src\ObjectBuilder-1.0.51206.0-source\ObjectBuilder\BuilderStrategy.cs:line 39
at Microsoft.Practices.ObjectBuilder.SingletonStrategy.BuildUp(IBuilderContext context, Type typeToBuild, Object existing, String idToBuild) in C:\EntLib3Src\ObjectBuilder-1.0.51206.0-source\ObjectBuilder\Strategies\Singleton\SingletonStrategy.cs:line 48
at Microsoft.Practices.ObjectBuilder.BuilderStrategy.BuildUp(IBuilderContext context, Type typeToBuild, Object existing, String idToBuild) in C:\EntLib3Src\ObjectBuilder-1.0.51206.0-source\ObjectBuilder\BuilderStrategy.cs:line 39
at Microsoft.Practices.EnterpriseLibrary.Common.Configuration.ObjectBuilder.ConfigurationNameMappingStrategy.BuildUp(IBuilderContext context, Type t, Object existing, String id)
at Microsoft.Practices.ObjectBuilder.BuilderBase`1.DoBuildUp(IReadWriteLocator locator, Type typeToBuild, String idToBuild, Object existing, PolicyList[] transientPolicies) in C:\EntLib3Src\ObjectBuilder-1.0.51206.0-source\ObjectBuilder\BuilderBase.cs:line 101
at Microsoft.Practices.ObjectBuilder.BuilderBase`1.BuildUp(IReadWriteLocator locator, Type typeToBuild, String idToBuild, Object existing, PolicyList[] transientPolicies) in C:\EntLib3Src\ObjectBuilder-1.0.51206.0-source\ObjectBuilder\BuilderBase.cs:line 79
at Microsoft.Practices.ObjectBuilder.BuilderBase`1.BuildUpTTypeToBuild(IReadWriteLocator locator, String idToBuild, Object existing, PolicyList[] transientPolicies) in C:\EntLib3Src\ObjectBuilder-1.0.51206.0-source\ObjectBuilder\BuilderBase.cs:line 66
at Microsoft.Practices.EnterpriseLibrary.Common.Configuration.ObjectBuilder.EnterpriseLibraryFactory.BuildUpT(IReadWriteLocator locator, String id, IConfigurationSource configurationSource)
at Microsoft.Practices.EnterpriseLibrary.Common.Configuration.ObjectBuilder.LocatorNameTypeFactoryBase`1.Create(String name)
at Austock.ExceptionHandling.ExceptionPolicy.HandleException(Exception exception, String policyName) in C:\Source\Austock\Austock.ExceptionHandling\ExceptionPolicy.cs:line 146

===================================

Request for the permission of type 'System.Security.Permissions.FileIOPermission, mscorlib, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089' failed. (mscorlib)

------------------------------
Program Location:

at System.Security.CodeAccessSecurityEngine.Check(Object demand, StackCrawlMark& stackMark, Boolean isPermSet)
at System.Security.CodeAccessPermission.Demand()
at System.IO.FileStream.Init(String path, FileMode mode, FileAccess access, Int32 rights, Boolean useRights, FileShare share, Int32 bufferSize, FileOptions options, SECURITY_ATTRIBUTES secAttrs, String msgPath, Boolean bFromProxy)
at System.IO.FileStream..ctor(String path, FileMode mode, FileAccess access, FileShare share)
at System.Configuration.Internal.InternalConfigHost.StaticOpenStreamForRead(String streamName)
at System.Configuration.Internal.InternalConfigHost.System.Configuration.Internal.IInternalConfigHost.OpenStreamForRead(String streamName, Boolean assertPermissions)
at System.Configuration.Internal.InternalConfigHost.System.Configuration.Internal.IInternalConfigHost.OpenStreamForRead(String streamName)
at System.Configuration.ClientConfigurationHost.OpenStreamForRead(String streamName)
at System.Configuration.UpdateConfigHost.OpenStreamForRead(String streamName)
at System.Configuration.BaseConfigurationRecord.FindSection(String[] keys, SectionXmlInfo sectionXmlInfo, Int32& lineNumber)
at System.Configuration.BaseConfigurationRecord.GetSectionXmlReader(String[] keys, SectionInput input)


Why is it looking for the config file in C:\Documents and Settings\dkerry\Local Settings\Application Data\assembly\dl3NJ5TCO7T.77O\55R92G3X.PXH\6ffabc3a\d0501f65_b21fc801\ folder rather than the bin\debug or bin\release folder?

Aslo, when i did F5 to run the application it didn't create a copy of the config file in "C:\Documents and Settings\dkerry\Local Settings\Application Data\assembly\dl3\NJ5TCO7T.77O\55R92G3X.PXH\6ffabc3a\d0501f65_b21fc801\" folder, i had to manually copy it there.

I just realised that it is not the Class Library project it is failing in, it is causing these issue in VSTO project, document level or application level. It is working fine for other types of projects.

{quote}
fsimonazzi wrote:
Hi,

Unless you do something explicit about it (like using the .net config console to specify custom security policies), class libraries and the winforms application that uses them should have the same trust settings; there has to be more to your scenario than the winform app/class library nature of the caller.

My guess is you're using the signed EntLib binaries with a web app running in medium trust, but this detail doesn't really matter. These binaries do not have the APTCA (see http://blogs.msdn.com/shawnfa/archive/2005/02/04/367390.aspx for details), so they cannot be used for partially trusted applications. You can use EntLib for these apps if you build your own, unsigned version of the library. This is unfortunately not as simple as it should, as the ObjectBuilder assembly used by EntLib is shipped as a binary; this thread [url:http://www.codeplex.com/e