Reading configuration section Failed

孤者浪人 提交于 2019-12-06 11:32:18

问题


I'm having a problem with some application. When I execute it in local disk all is right, but when I try to execute the same application from a shared resource (Z:\resource\TheApplication.exe) I got the following error:

Error occurred creating the configuration section handler for DOMAIN/DomainUserInfo: Request failed

The error occurs when trying to read a custom section in the configuration file:

public static class AppConfigFile {
    public static IDomainUserInfo DomainUserInfo {
        get {
            if (_domainUserInfo == null) {
                _domainUserInfo = (DomainUserInfo)ConfigurationManager.GetSection(Environment.UserDomainName + @"/DomainUserInfo");         
            } return _domainUserInfo as IDomainUserInfo;
        }
    }
}
public class DomainUserInfo : ConfigurationSection, IDomainUserInfo {
    [ConfigurationProperty("SomeConfiguration", IsRequired = true, DefaultValue = "")]
    public string SomeConfiguration { get { return (string)base["SomeConfiguration"]; } }    

    [ConfigurationProperty("OtherConfiguration", IsRequired = true, DefaultValue = "")]
    public string OtherConfiguration { get { return (string)base["OtherConfiguration"]; } }    
}

The configuration file looks like:

<configuration>
  <configSections>
    <sectionGroup name="THE_DOMAIN" type="System.Configuration.NameValueSectionHandler, System, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089">
      <section name="DomainUserInfo" type="NameSpace.Other.DomainUserInfo,NameSpace.Other" requirePermission="false"/>
    </sectionGroup>
  </configSections>

    <THE_DOMAIN>
        <DomainUserInfo SomeConfiguration="SomeValue" OtherConfiguration="OtherValue"></DomainUserInfo>
    </THE_DOMAIN>
</configuration>

Any ideas?

Thanks.


回答1:


The problem you have described is caused by a bug in application configuration functionality of .NET 4.0.

Edit

Link to bug is not available anymore approximately since release of Visual Studio 2012 due to re-design of Microsoft Connect website. I did not manage to find defect on new Microsoft Connect anymore and do not remember its number.

The only thing I have managed to find is that link to that bug still remains in Google's cache.

As a work around it helped for me to open Application Configuration with code:

configuration = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);

and then read custom section with following code:

CustomSection1 customSection1 = (CustomSection1)configuration.GetSection("CustomSection1");

Note : Work around was found by me in answer to similar question.




回答2:


You haven't declared OtherConfiguration in your class. Therefore, you cannot have it as an attribute on the DomainUserInfo in your config.



来源:https://stackoverflow.com/questions/4444847/reading-configuration-section-failed

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!