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:
There's a couple of things wrong with your code.
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.
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.
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"));
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With