ConfigurationSection ConfigurationManager.GetSection() always returns null

Jon picture Jon · May 17, 2011 · Viewed 25.9k times · Source

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)
    {           
        StandardConfigSectionHandler 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/

Answer

Markus Olsson picture Markus Olsson · May 17, 2011

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