ConfigurationSection ConfigurationManager.GetSection() always returns null
Asked Answered
D

2

10

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/
Dibranchiate answered 17/5, 2011 at 21:5 Comment(0)
D
10

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

Drachm answered 17/5, 2011 at 21:43 Comment(2)
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. ThanksDibranchiate
@Markus - I believe that the set accessor for the value property in your class is useless because configuration information cannot be directly written to.Deyo
C
1

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"));
Cancroid answered 15/3, 2016 at 12:53 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.