ConfigurationSection ConfigurationManager.GetSection() always returns null

26,165

Solution 1

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

Solution 2

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"));
Share:
26,165
Jon
Author by

Jon

C# Developer in the London. Loves C# and being challenged with new and interesting projects. My github account is: https://github.com/twistedtwig

Updated on July 12, 2022

Comments

  • Jon
    Jon almost 2 years

    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/
  • Jon
    Jon about 13 years
    Hi Markus, the return null was during a test and I forgot to remove it, but that was not the issue, the "secion" object was null. I did say in my post I tried various configPath's. Your third point was right on the money and a REAL help. I had read the link before but had just got my self too confused. Looking at it now I get it and have created a subConfigurationSection (nested) and all works well. Thanks
  • RoastBeast
    RoastBeast over 10 years
    @Markus - I believe that the set accessor for the value property in your class is useless because configuration information cannot be directly written to.