【问题标题】:ConfigurationSection ConfigurationManager.GetSection() always returns nullConfigurationSection ConfigurationManager.GetSection() 始终返回 null
【发布时间】:2016-03-15 20:53:19
【问题描述】:

我正在尝试学习如何使用 ConfigurationSection 类。我曾经使用 IConfigurationSectionHandler,但发布它已被贬值。所以作为一个好孩子,我正在尝试“正确”的方式。我的问题是它总是返回 null。

我有一个控制台应用程序和一个 DLL。

class Program
{
    static void Main(string[] args)
    {           
        StandardConfigSectionHandler section = StandardConfigSectionHandler.GetConfiguration();

        string value = section.Value;
    }
}

应用配置:

<?xml version="1.0" encoding="utf-8" ?>
<configuration>

  <configSections>
    <sectionGroup name="ConfigSectionGroup">
      <section name="ConfigSection" type="Controller.StandardConfigSectionHandler, Controller" />
    </sectionGroup>
  </configSections>

  <ConfigSectionGroup>
    <ConfigSection>
      <test value="1" />
    </ConfigSection>
  </ConfigSectionGroup>

</configuration>

DLL 中的节处理程序:

namespace Controller
{    
    public class StandardConfigSectionHandler : ConfigurationSection
    {
    private const string ConfigPath = "ConfigSectionGroup/ConfigSection/";

    public static StandardConfigSectionHandler GetConfiguration()
    {
        object section = ConfigurationManager.GetSection(ConfigPath);
        return section as StandardWcfConfigSectionHandler;
    }

    [ConfigurationProperty("value")]
    public string Value
    {
        get { return (string)this["value"]; }
        set { this["value"] = value; }
    }
  }
}

我为“ConfigPath”尝试的任何值都将返回 null,或者抛出一个错误,说“test”是一个无法识别的元素。我尝试过的值:

  • 配置节组
  • ConfigSectionGroup/
  • ConfigSectionGroup/ConfigSection
  • ConfigSectionGroup/ConfigSection/
  • ConfigSectionGroup/ConfigSection/test
  • ConfigSectionGroup/ConfigSection/test/

【问题讨论】:

    标签: c# configurationmanager configurationsection


    【解决方案1】:

    您的代码有几处问题。

    1. 您总是在您的 GetConfiguration 方法中返回 null,但我会假设这只是在问题中,而不是在您的实际代码中。

    2. 更重要的是,ConfigPath 值的格式不正确。你有一个尾部斜杠ConfigSectionGroup/ConfigSection/,删除最后一个斜杠,它就能找到该部分。

    3. 最重要的是,您声明部分的方式配置系统将期望您的“值”存储在 ConfigSection 元素的属性中。像这样

      <ConfigSectionGroup>
        <ConfigSection value="foo" />
      </ConfigSectionGroup>
      

    所以,把它们放在一起:

    public class StandardConfigSectionHandler : ConfigurationSection
    {
        private const string ConfigPath = "ConfigSectionGroup/ConfigSection";
    
        public static StandardConfigSectionHandler GetConfiguration()
        {
            return (StandardConfigSectionHandler)ConfigurationManager.GetSection(ConfigPath);
        }
    
        [ConfigurationProperty("value")]
        public string Value
        {
            get { return (string)this["value"]; }
            set { this["value"] = value; }
        }
    }
    

    要了解有关如何配置配置部分的更多信息,请参阅此出色的 MSDN 文档:How to: Create Custom Configuration Sections Using ConfigurationSection。它还包含有关如何将配置值存储在(如您的测试元素)的子元素中的信息。

    【讨论】:

    • 嗨,Markus,返回 null 是在测试期间,我忘了删除它,但这不是问题,“secion”对象为 null。我确实在我的帖子中说过我尝试了各种 configPath。你的第三点是正确的钱和真正的帮助。我之前读过这个链接,但只是让我自己太困惑了。现在看它,我明白了,并创建了一个 subConfigurationSection(嵌套)并且一切正常。谢谢
    • @Markus - 我相信你的类中 value 属性的 set 访问器是无用的,因为配置信息不能直接写入。
    【解决方案2】:

    我也有类似的问题:

    ConfigurationManager.GetSection("CompaniesSettings")
    

    我的配置文件:

        <section name="CompaniesSettings" type="Swedbank2015.CompaniesSectionReader, Swedbank2015"/>
    

    我遇到了一个错误:

    无法加载文件或程序集“Swedbank2015”

    我发现了一个有趣的解决方案,我将类文件移到了一个单独的项目中(类型 = 类库,名称 = SwBankConfigHelper)。我将它添加到参考并更改配置文件:

    <section name="CompaniesSettings" type=" SwBankConfigHelper.CompaniesSectionReader, SwBankConfigHelper"/>
    

    我的代码运行良好!

    CompaniesConfig = new CompaniesConfig((XmlNodeList)ConfigurationManager.GetSection("CompaniesSettings"));
    

    【讨论】:

      猜你喜欢
      • 2012-08-06
      • 2016-05-31
      • 2015-08-15
      • 2012-03-18
      • 2016-11-04
      • 2016-07-28
      • 2010-11-08
      • 2017-11-26
      • 2015-02-23
      相关资源
      最近更新 更多