Search code examples
c#configurationconfigurationmanager

ConfigurationManager.GetSection doesn't return custom section


Because my new project has a complex configuration structure I'm learning how to use class ConfigurationManager with Custom Configurations.

To do this, I used MSDN How to: Create Custom Configuration Sections Using ConfigurationSection as an example. This example shows a configuration section for a class that aggregates other configurable classes.

MyProblem: ConfigurationManager.GetSection does not return a section

I created a ConsoleApplication.

  • Namespace: TryConfigManagement
  • Add reference: System.Configuration
  • Changed App.Config (copy always) as follows:

    <?xml version="1.0" encoding="utf-8"?>
    <configuration>
    
      <!-- Configuration section-handler declaration area.  -->
      <configSections>
        <sectionGroup name="pageAppearanceGroup">
          <section
            name="pageAppearance"
            type="TryConfigManagement.PageAppearance"
            allowLocation="true"
            allowDefinition="Everywhere"
            />
        </sectionGroup>
      </configSections>
    
      <!-- Configuration section settings area. -->
      <pageAppearanceGroup>
        <pageAppearance remoteOnly="true">
          <font name="TimeNewRoman" size="18"/>
          <color background="000000" foreground="FFFFFF"/>
        </pageAppearance>  
      </pageAppearanceGroup>
    
      <startup> 
        <supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.5.1"/>
      </startup>
    </configuration>
    

Note that I used a configuration group as in the example from MSDN.

My source code:

namespace TryConfigManagement
{
    class Program
    {
        static void Main(string[] args)
        {
            string sectionName = "pageAppearanceGroup/pageAppearance";
            object section = ConfigurationManager.GetSection(sectionName);

This piece of code throws the following exception:

An error occurred creating the configuration section handler
for pageAppearanceGroup/pageAppearance:
Could not load type 'TryConfigManagement.PageAppearanceSection'
from assembly 'System.Configuration, Version=4.0.0.0,
Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a'.
(C:\...\bin\Debug\TryConfigManagement.vshost.exe.config line 6)

Although I did exactly what was in the example I figured out from StackOverflow that I should add the assembly information, probably like below:

class Program
{
    static void Main(string[] args)
    {
        var assembly = Assembly.GetExecutingAssembly();
        string sectionName = "pageAppearanceGroup/pageAppearance ," +
            assembly.FullName;
        object section = ConfigurationManager.GetSection(sectionName);

Now the exception is not thrown anymore, but the returned value is null.

I think it has to do with the type in the section name of the config file. However trying different values like adding full assembly name didn't help.

Alas I can't find a lot of information about the string parameter in GetSection.

So what is wrong with my app.config, or with my GetSection call?


Solution

  • You can try to specify the assembly where TryConfigManagement.PageAppearance type is located in your config:

     <sectionGroup name="pageAppearanceGroup">
          <section
            name="pageAppearance"
            type="TryConfigManagement.PageAppearance, TryConfigManagement"
            allowLocation="true"
            allowDefinition="Everywhere"
            />
        </sectionGroup>
    

    Then don't include the assembly name in GetSection parameter.

    Also, remove the space after your section name.