c# - ConfigurationSection ConfigurationManager.GetSection() 总是返回 null

标签 c# configurationmanager configurationsection

我正在尝试学习如何使用 ConfigurationSection 类。我曾经使用 IConfigurationSectionHandler 但发布它已被折旧。所以作为一个好 child ,我正在尝试“正确”的方式。我的问题是它总是返回 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/
  • ConfigSectionGroup/ConfigSection
  • ConfigSectionGroup/ConfigSection/
  • ConfigSectionGroup/ConfigSection/测试
  • ConfigSectionGroup/ConfigSection/test/

最佳答案

您的代码有几个问题。

  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 .它还包含有关如何在(如您的测试元素)的子元素中存储配置值的信息。

关于c# - ConfigurationSection ConfigurationManager.GetSection() 总是返回 null,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6037136/

相关文章:

c# - 克隆通用 ConfigurationSection 而不使用自定义类

c# - 如何保存带有自定义配置部分的配置文件?

configuration - 读取自定义配置部分时出错 : No parameterless constructor defined for this object

c# - 在 Configuration.Save() 上保留格式

c# - 可以自动增加DLL版本

c# - 最小生成树快速图表

c# - 访问另一个项目的 app.config 属性?

.net - 如何在后代元素上使用 .NET 自定义 ConfigurationElement 属性?

c# - 调用存储过程或运行sql查询?

c# - 命名空间 'System' 中不存在“DirectoryServices”