Search code examples
c#app-configconfigurationsection

Reading app.config section name


In console application I'm trying to get section key value pair but I couldn't. There are two ways to do this:

  1. ConfigurationSection
  2. IConfigurationSectionHandler

But the code below is generating this error: Configuration system failed to initialize Where am I making mistake?

Here is the code: app.config

<?xml version="1.0"?>
<configuration>
  <configSections>
    <sectionGroup name="testGroupName">
      <section name="secName" type="TestAssemblyConfigReader.TestConfigReader"/>
    </sectionGroup>
  </configSections>

  <testGrupAdi>
    <secName anahtar="deger" />
  </testGrupAdi>
</configuration>

Config reader class:

using System.Configuration;

namespace TestAssemblyConfigReader
{
    public class TestConfigReader : ConfigurationSection
    {
        public TestConfigReader()
        {

        }

        [ConfigurationProperty("secName")]
        public string SecName
        {
            get
            {
                return (string)this["secName"];
            }
            set
            {
                this["secName"] = value;
            }
        }
    }
}

Console Application:

using System.Configuration;
using TestAssemblyConfigReader;

namespace ca_FMC.Turkiye.Lib.SVN
{
    class Program
    {
        static void Main(string[] args)
        {
            TestConfigReader serviceConfigSection = ConfigurationManager.GetSection("testGroupName") as TestConfigReader;
        }
    }
}

Solution

  • Hello remove mismatch between testGroupName and testGrupAdi

    use this

    <configuration>
      <configSections>
        <sectionGroup name="testGroupName">
          <section name="secName" type="TestAssemblyConfigReader.TestConfigReader"/>
        </sectionGroup>
      </configSections>
    
      <testGroupName>
        <secName anahtar="deger" />
      </testGroupName>
    </configuration>