none
ConfigurationErrorsException - Migrating from 3.5 to 4.0

    Question

  • I have a console application that consumes a dll, this dll needs some configuration data stored in the config file which obviously has to be at in the config file for the exe (console).

    The dll is attempting to execute a ConfigurationManager.GetSection(string) command, it seems like the new security model is causing issues to my process. The console executable is been launched from the network which usually required the CAS policies to be adjusted and they are for the 2.0, in 4.0 they have been removed and I should not be having this issue but apparently that is not the case. What I'm doing wrong?

     The inner exception points to a SecurityException reading the config file or something along those lines. Below is the full exception. Same code built in 3.5 does not cause issues.

    System.Configuration.ConfigurationErrorsException: An error occurred creating the configu
    ration section handler for overrideConfigurations: Request failed. (R:\Development\Junk\n
    ewConsole\TestConsoleApp.exe.Config line 5) ---> System.Security.SecurityException: Reque
    st failed.
       at System.RuntimeTypeHandle.CreateInstance(RuntimeType type, Boolean publicOnly, Boole
    an noCheck, Boolean& canBeCached, RuntimeMethodHandleInternal& ctor, Boolean& bNeedSecuri
    tyCheck)
       at System.RuntimeType.CreateInstanceSlow(Boolean publicOnly, Boolean skipCheckThis, Bo
    olean fillCache)
       at System.RuntimeType.CreateInstanceDefaultCtor(Boolean publicOnly, Boolean skipVisibi
    lityChecks, Boolean skipCheckThis, Boolean fillCache)
       at System.Activator.CreateInstance(Type type, Boolean nonPublic)
       at System.Configuration.TypeUtil.CreateInstanceWithReflectionPermission(Type type)
       at System.Configuration.RuntimeConfigurationRecord.RuntimeConfigurationFactory.Init(Ru
    ntimeConfigurationRecord configRecord, FactoryRecord factoryRecord)
       at System.Configuration.RuntimeConfigurationRecord.RuntimeConfigurationFactory.InitWit
    hRestrictedPermissions(RuntimeConfigurationRecord configRecord, FactoryRecord factoryReco
    rd)
       at System.Configuration.RuntimeConfigurationRecord.CreateSectionFactory(FactoryRecord
    factoryRecord)
       at System.Configuration.BaseConfigurationRecord.FindAndEnsureFactoryRecord(String conf
    igKey, Boolean& isRootDeclaredHere)
       --- End of inner exception stack trace ---
       at System.Configuration.BaseConfigurationRecord.FindAndEnsureFactoryRecord(String conf
    igKey, Boolean& isRootDeclaredHere)
       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)
       at System.Configuration.ClientConfigurationSystem.System.Configuration.Internal.IInter
    nalConfigSystem.GetSection(String sectionName)
       at System.Configuration.ConfigurationManager.GetSection(String sectionName)
       at CadreDummy.Class1..ctor() in E:\MainLine\Mainline\Unsecure\CRS\CadreDummy\Class1.cs
    :line 17


    Fabian
    Friday, July 02, 2010 3:43 PM

Answers

  • It is actually a known issue of .NET 4.0, it has been fixed and it is planned to be available in next .NET Framework release.

    Currently, you may use following code as a workaround:

     

                    var config = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);

                    var junk = config.GetSection("overrides/applicationSettings");


    Sincerely,
    Eric
    MSDN Subscriber Support in Forum
    If you have any feedback of our support, please contact msdnmg@microsoft.com.
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.
    Welcome to the All-In-One Code Framework! If you have any feedback, please tell us.

     

    Tuesday, August 03, 2010 2:09 AM

All replies

  •  

    Hi,

    I tried to reproduce this issue by putting an exe (.net 4.0) and its dependency dll (.net 4.0) in a network share folder and run the exe, but the dll executes ConfigurationManager.GetSection() method successfully, I didn't get the security exception. If this issue is stably reproducible, would you please provide some reproduce steps?

     

    By the way, what is the inner security exception details?


    Sincerely,
    Eric
    MSDN Subscriber Support in Forum
    If you have any feedback of our support, please contact msdnmg@microsoft.com.
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.
    Welcome to the All-In-One Code Framework! If you have any feedback, please tell us.
    Monday, July 05, 2010 6:06 AM
  • in my environment I can reproduce every time. If I switch both projects to 3.5, it stops happening, in 4.0 it happends every time. Only when I run from the network. I can send you some code if you would like.

    The inner exception has the following information:

    Action: System.Security.Permissions.SecurityAction.Demand

    Demanded: {<PermissionSet class="System.Security.PermissionSet" version="1" Unrestricted="true"/}

    GrantedSet: ""

    Method: {System.Object CreateSectionWithRestrictedPermissions(System.Configuration.RuntimeConfigurationRecord, System.Configuration.FactoryRecord, System.Configuration.SectionRecord, System.Object, System.Configuration.ConfigXmlReader)}

    PermissionState: <PermissionSet class="System.Security.PermissionSet" version="1" Unrestricted="true"/>


    PermissionType: {Name = "PermissionSet" FullName = "System.Security.PermissionSet"}

    PermitOnlySetInstance: {<PermissionSet class="System.Security.PermissionSet" version="1">
    <IPermission class="System.Security.Permissions.EnvironmentPermission, mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" version="1" Read="USERNAME"/>
    <IPermission class="System.Security.Permissions.FileDialogPermission, mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" version="1"
    Unrestricted="true"/>
    <IPermission class="System.Security.Permissions.FileIOPermission, mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" version="1"
    Read="R:\Development\Junk\newConsole\" PathDiscovery="R:\Development\Junk\newConsole\"/>
    <IPermission class="System.Security.Permissions.IsolatedStorageFilePermission, mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089"
    version="1" Allowed="AssemblyIsolationByUser" UserQuota="9223372036854775807" Expiry="9223372036854775807" Permanent="True"/>
    <IPermission class="System.Security.Permissions.ReflectionPermission, mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089"
    version="1" Flags="ReflectionEmit, RestrictedMemberAccess"/>
    <IPermission class="System.Security.Permissions.SecurityPermission, mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089"
    version="1" Flags="Assertion, Execution, BindingRedirects"/>
    <IPermission class="System.Security.Permissions.UIPermission, mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089"
    version="1" Unrestricted="true"/>
    <IPermission class="System.Drawing.Printing.PrintingPermission, System.Drawing, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a"
    version="1" Level="DefaultPrinting"/>
    <IPermission class="System.Net.DnsPermission, System, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089"
    version="1" Unrestricted="true"/>
    <IPermission class="System.Security.Permissions.TypeDescriptorPermission, System, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089"
    version="1" Unrestricted="true"/>
    <IPermission class="System.Security.Permissions.MediaPermission, WindowsBase, Version=4.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35"
    version="1" Audio="SafeAudio" Video="SafeVideo" Image="SafeImage"/>
    <IPermission class="System.Security.Permissions.WebBrowserPermission, WindowsBase, Version=4.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35"
    version="1" Level="Safe"/>
    </PermissionSet>
    }

    Stack Trace:    at System.RuntimeMethodHandle.PerformSecurityCheck(Object obj, RuntimeMethodHandleInternal method, RuntimeType parent, UInt32 invocationFlags)
       at System.RuntimeMethodHandle.PerformSecurityCheck(Object obj, IRuntimeMethodInfo method, RuntimeType parent, UInt32 invocationFlags)
       at System.Reflection.RuntimeConstructorInfo.Invoke(BindingFlags invokeAttr, Binder binder, Object[] parameters, CultureInfo culture)
       at System.Reflection.ConstructorInfo.Invoke(Object[] parameters)
       at System.Configuration.TypeUtil.InvokeCtorWithReflectionPermission(ConstructorInfo ctor)
       at System.Configuration.RuntimeConfigurationRecord.RuntimeConfigurationFactory.CreateSectionImpl(RuntimeConfigurationRecord configRecord, FactoryRecord factoryRecord, SectionRecord sectionRecord, Object parentConfig, ConfigXmlReader reader)
       at System.Configuration.RuntimeConfigurationRecord.RuntimeConfigurationFactory.CreateSectionWithRestrictedPermissions(RuntimeConfigurationRecord configRecord, FactoryRecord factoryRecord, SectionRecord sectionRecord, Object parentConfig, ConfigXmlReader reader)
       at System.Configuration.RuntimeConfigurationRecord.CreateSection(Boolean inputIsTrusted, FactoryRecord factoryRecord, SectionRecord sectionRecord, Object parentConfig, ConfigXmlReader reader)
       at System.Configuration.BaseConfigurationRecord.CallCreateSection(Boolean inputIsTrusted, FactoryRecord factoryRecord, SectionRecord sectionRecord, Object parentConfig, ConfigXmlReader reader, String filename, Int32 line)

     TargetSite: {Void PerformSecurityCheck(System.Object, System.RuntimeMethodHandleInternal, System.RuntimeType, UInt32)}

     Zone:  System.Security.SecurityZone.NoZone

    Message: An error occurred creating the configuration section handler for overrides/applicationSettings: Request failed. (R:\Development\Junk\newConsole\TestConsoleApp.exe.Config line 14)

    StackTrace:    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.GetSectionRecursive(String configKey, Boolean getLkg, Boolean checkPermission, Boolean getRuntimeObject, Boolean requestIsHere, 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)
       at System.Configuration.ClientConfigurationSystem.System.Configuration.Internal.IInternalConfigSystem.GetSection(String sectionName)
       at System.Configuration.ConfigurationManager.GetSection(String sectionName)
       at CadreDummy.Class1..ctor() in E:\MainLine\Mainline\Unsecure\CRS\CadreDummy\Class1.cs:line 22
       at TestConsoleApp.Program.Main(String[] args) in E:\MainLine\Mainline\Unsecure\CRS\TestConsoleApp\Program.cs:line 10

     

    Thanks for any help

    Fabian


    Fabian
    Tuesday, July 06, 2010 6:15 PM
  • It also stops happening if I include this in the dll that invokes the getSection code:

    [assembly: System.Security.SecurityRules(System.Security.SecurityRuleSet.Level1)]


    Fabian
    Tuesday, July 06, 2010 6:17 PM
  • To make matters more interesting, if I run the following code:

    Assembly.GetAssembly(GetType()).IsFullyTrusted

    I get true in both the console application and the dll that is running the GetSection() code. Also, if I run the following code in both dll and console:

    new PermissionSet(PermissionState.Unrestricted).Demand();

    I also don't get any exceptions, so it seems like I'm running in full trust.

     

     

     

     

     


    Fabian
    Tuesday, July 06, 2010 7:27 PM
  •  

    I found a similar issue at here, which also provide a workaround solution, you may want to have a read.

     

    It will be very helpful if you can create a small reproduce project and send it to us, after you generate the project, please let me know your email address by sending a mail to v-eryang@microsoft.com. Then I will create a file transfer workspace where you can upload your files. The file will be kept confidential.


    Sincerely,
    Eric
    MSDN Subscriber Support in Forum
    If you have any feedback of our support, please contact msdnmg@microsoft.com.
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.
    Welcome to the All-In-One Code Framework! If you have any feedback, please tell us.
    Wednesday, July 07, 2010 3:46 AM
  • You can add that, but what you really need to add is
    [assemblyAllowPartiallyTrustedCallers]

    The configuration manager appears to go down different code paths based on whether the assembly containing the configuration section class has that attribute or not. 

    If you add that attribute, then by default you'll be Level1.  You can also specify Level2 if you'd like.

    • Proposed as answer by Oren Novotny Monday, August 02, 2010 6:57 PM
    Monday, August 02, 2010 6:56 PM
  • It is actually a known issue of .NET 4.0, it has been fixed and it is planned to be available in next .NET Framework release.

    Currently, you may use following code as a workaround:

     

                    var config = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);

                    var junk = config.GetSection("overrides/applicationSettings");


    Sincerely,
    Eric
    MSDN Subscriber Support in Forum
    If you have any feedback of our support, please contact msdnmg@microsoft.com.
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.
    Welcome to the All-In-One Code Framework! If you have any feedback, please tell us.

     

    Tuesday, August 03, 2010 2:09 AM
  • Can you post the issue number on Connect?

     

    Thanks a lot.

    Juan Zamudio

    Thursday, October 14, 2010 11:57 PM
  • I am running in to the same issue and wondering if this issue is fixed or not yet. Please advise.

     

     

    Tuesday, April 12, 2011 7:02 PM
  • I have the Framework 4 with SP1 and I'm still getting this error.

     

     

    Tuesday, August 02, 2011 8:06 PM
  • Hi

    Can you please point me to a URL where I can get this KB if they have already released it by now.

    Thanks in advance

    Monday, July 23, 2012 11:09 AM
  • I have downloaded .Net 4.0.3 and installed it .. but still no success.. Am I missing something..

    I have visual studio 2010 installed for my dev environment

    Monday, July 23, 2012 11:20 AM