简体   繁体   English

ConfigurationSection ConfigurationManager.GetSection()始终返回null

[英]ConfigurationSection ConfigurationManager.GetSection() always returns null

I am trying to learn how to use the ConfigurationSection class. 我正在尝试学习如何使用ConfigurationSection类。 I used to use the IConfigurationSectionHandler but released that it has been depreciated. 我曾经使用IConfigurationSectionHandler,但发布它已被折旧。 So being a good lad I am trying the "correct" way. 因此,作为一个好孩子,我正在尝试“正确”的方式。 My problem is that it is always returning null. 我的问题是它总是返回null。

I have a console app and a DLL. 我有一个控制台应用程序和DLL。

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

        string value = section.Value;
    }
}

app config: app配置:

<?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>

section handler in DLL: DLL中的section处理程序:

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; }
    }
  }
}

What ever values I try for the "ConfigPath" it will return null, or throw an error saying "test" is an unrecognised element. 什么值我尝试“ConfigPath”它将返回null,或抛出错误说“test”是一个无法识别的元素。 Values I tried: 我试过的价值观:

  • ConfigSectionGroup ConfigSectionGroup
  • ConfigSectionGroup/ ConfigSectionGroup /
  • ConfigSectionGroup/ConfigSection ConfigSectionGroup / ConfigSection
  • ConfigSectionGroup/ConfigSection/ ConfigSectionGroup / ConfigSection /
  • ConfigSectionGroup/ConfigSection/test ConfigSectionGroup / ConfigSection /测试
  • ConfigSectionGroup/ConfigSection/test/ ConfigSectionGroup / ConfigSection /测试/

There's a couple of things wrong with your code. 您的代码存在一些问题。

  1. You're always returning null in your GetConfiguration method but I'm going to assume that's just in the question and not in your actual code. 你总是在你的GetConfiguration方法中返回null ,但我会假设这只是问题,而不是你的实际代码。

  2. More importantly, the format of the ConfigPath value is incorrect. 更重要的是, ConfigPath值的格式不正确。 You have a trailing slash ConfigSectionGroup/ConfigSection/ , remove the last slash and it'll be able to find the section. 你有一个尾部斜杠ConfigSectionGroup/ConfigSection/ ,删除最后一个斜杠,它将能够找到该部分。

  3. Most important, the way you've declared your section the configuration system will expect your "value" be stored in an attribute of your ConfigSection element. 最重要的是,您在配置系统中声明您的部分的方式将期望您的“值”存储在ConfigSection元素的属性中。 Like this 像这样

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

So, putting it all together: 所以,把它们放在一起:

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; }
    }
}

To read more about how you configure configuration sections please consult this excellent MSDN documentation: How to: Create Custom Configuration Sections Using ConfigurationSection . 要阅读有关如何配置配置部分的更多信息,请参阅此优秀的MSDN文档: 如何:使用ConfigurationSection创建自定义配置部分 It also contains information about how to store configuration values in sub-elements of (like your test element). 它还包含有关如何在(与测试元素)的子元素中存储配置值的信息。

I was similar problem with: 我遇到了类似的问题:

ConfigurationManager.GetSection("CompaniesSettings")

My configuration file: 我的配置文件:

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

I got an error: 我收到一个错误:

Could not load file or assembly 'Swedbank2015' 无法加载文件或程序集'Swedbank2015'

I was found interesting solution, I moved the class file in a separate project (type = Class Library, name = SwBankConfigHelper) . 我找到了有趣的解决方案,我将类文件移动到一个单独的项目中(type = Class Library,name = SwBankConfigHelper)。 I added it to Reference and change configuration file: 我将它添加到参考并更改配置文件:

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

And my code works fine! 我的代码工作正常!

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 ConfigurationManager.GetSection 返回 null - ConfigurationManager.GetSection returns null ConfigurationManager.GetSection(sectionName)在执行单元测试时返回null - ConfigurationManager.GetSection(sectionName) returns null while performing unit tests ConfigurationManager.GetSection()函数 - ConfigurationManager.GetSection() function ConfigurationManager.GetSection始终为对象提供默认值 - ConfigurationManager.GetSection always gives object with default values ConfigurationManager.GetSection跳过重复项 - ConfigurationManager.GetSection Skips Duplicates ReadOnlyNameValueCollection(从ConfigurationManager.GetSection读取) - ReadOnlyNameValueCollection (reading from ConfigurationManager.GetSection) ConfigurationManager.GetSection和Configuration.GetSection有什么区别? - What is Difference between ConfigurationManager.GetSection and Configuration.GetSection? ConfigurationManager.GetSection不返回自定义部分 - ConfigurationManager.GetSection doesn't return custom section 如何在.Net Core 2.2中正确使用ConfigurationManager.GetSection() - How to use properly ConfigurationManager.GetSection() in .Net Core 2.2 为什么ConfigurationManager.GetSection“ system.webServer / handlers”不可用? - Why is the ConfigurationManager.GetSection “system.webServer/handlers” not available?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM