10

我正在尝试学习如何使用 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/ConfigSection
  • ConfigSectionGroup/ConfigSection/
  • ConfigSectionGroup/ConfigSection/测试
  • ConfigSectionGroup/ConfigSection/test/
4

2 回答 2

10

您的代码有几处问题。

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

  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 文档:如何:使用 ConfigurationSection 创建自定义配置部分。它还包含有关如何将配置值存储在(如您的测试元素)的子元素中的信息。

于 2011-05-17T21:43:46.573 回答
1

我遇到了类似的问题:

ConfigurationManager.GetSection("CompaniesSettings")

我的配置文件:

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

我收到一个错误:

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

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

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

我的代码工作正常!

CompaniesConfig = new CompaniesConfig((XmlNodeList)ConfigurationManager.GetSection("CompaniesSettings"));
于 2016-03-15T12:53:19.137 回答