开发者

ConfigurationSection ConfigurationManager.GetSection() always returns null

I am trying to learn how to use the ConfigurationSection class. I used to use the IConfigurationSectionHandler but released that it has been depreciated. So being a good lad I am trying the "correct" way. My problem is that it is always returning null.

I have a console app and a DLL.

class Program
{
    static void Main(string[] args)
    {           
        Stand开发者_如何学运维ardConfigSectionHandler section = StandardConfigSectionHandler.GetConfiguration();

        string value = section.Value;
    }
}

app config:

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

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. Values I tried:

  • ConfigSectionGroup
  • ConfigSectionGroup/
  • ConfigSectionGroup/ConfigSection
  • ConfigSectionGroup/ConfigSection/
  • ConfigSectionGroup/ConfigSection/test
  • ConfigSectionGroup/ConfigSection/test/


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.

  2. More importantly, the format of the ConfigPath value is incorrect. You have a trailing slash ConfigSectionGroup/ConfigSection/, remove the last slash and it'll be able to find the section.

  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. 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. 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'

I was found interesting solution, I moved the class file in a separate project (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"));
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜