ConfigurationManager.GetSection() returns null, how to fix this

Viewed 1156

Maybe this question is already answered, but I haven't seen anything that works for me.

The issue is the following: I can't read from app.config and I don't know what I am doing wrong, basically the idea is to get the object CcgSettingCcg which is of Type CcgSettings. In the future, I plan to add other settings, always CcgSettings but with different name, lets say CcgSettingsCcg2 and so on,

from app.config:

<configSections>
        <sectionGroup name ="GeneralTenantEmailSettings">
            <section name="TenantEmailSettings" type="DataApiService.Models.TenantEmailSettings, DataApiService.Models" />
        </sectionGroup>
    </configSections>
    <TenantEmailSettings>
        <CcgSettingCcg SettingsId="520305DC-6E68-4FA3-B53B-18E25E0E21FF" TenantId="A0C55F11-516E-4F7C-9ECD-DEF7C47290B4" Frequency="168" StartDate="03/06/2020" RuleType="Default" To="Ccg" />
    </TenantEmailSettings>

Class definitions: I am using system.Configuration here

namespace DataApiService.Models{
public class CcgSettings : ConfigurationElement
    {
        [ConfigurationProperty("SettingsId", IsRequired=true)]
        public Guid SettingsId 
        {
            get
            {
                return (Guid)this["SettingsId"];
            }
            set
            {
                value = (Guid)this["SettingsId"];
            }
        }
        [ConfigurationProperty("TenantId", DefaultValue = null, IsRequired = false)]
        public Guid? TenantId
        {
            get
            {
                return (Guid)this["TenantId"];
            }
            set
            {
                value = (Guid)this["TenantId"];
            }
        }
        [ConfigurationProperty("Frequency", DefaultValue = 0,IsRequired = true)]
        public int Frequency
        {
            get
            {
                return (int)this["Frequency"];
            }
            set
            {
                value = (int)this["Frequency"];
            }
        }
        [ConfigurationProperty("StartDate", IsRequired = true)]
        public DateTime StartDate
        {
            get
            {
                return (DateTime)this["StartDate"];
            }
            set
            {
                value = (DateTime)this["StartDate"];
            }
        }
        [ConfigurationProperty("RuleType", DefaultValue = "default", IsRequired = true)]
        public string RuleType
        {
            get
            {
                return (string)this["RuleType"];
            }
            set
            {
                value = (string)this["RuleType"];
            }
        }
        [ConfigurationProperty("To", IsRequired = true)]
        public string To
        {
            get
            {
                return (string)this["To"];
            }
            set
            {
                value = (string)this["To"];
            }
        }
    }

    public class TenantEmailSettings: ConfigurationSection
    {
        [ConfigurationProperty("CcgSettings")]
        public CcgSettings CcgSettingsCcg
        {
            get
            {
                return (CcgSettings)this["CcgSettingCcg"];
            }
            set
            {
                value = (CcgSettings)this["CcgSettingCcg"];
            }
        }
    }
    public class GeneralTenantEmailSettings : ConfigurationSectionGroup
    {
        [ConfigurationProperty("TenantEmailSettings")]

        public TenantEmailSettings TenantEmailSettings
        {
            get; set;
        }
    }}

and finally this is where I try to read

    var t = ConfigurationManager.GetSection("TenantEmailSettings") as TenantEmailSettings; 
    var s = ConfigurationManager.GetSection("GeneralTenantSettings") as GeneralTenantEmailSettings;
    var c = ConfigurationManager.GetSection("CcgSettings") as CcgSettings;
    var b = ConfigurationManager.GetSection("CcgSettingsCcg") as CcgSettings;

t, s, c and b return null, this is my problem

2 Answers

Here is some sample code that should help, assuming you need to use app.config files. In order to make the example work you will need the following NuGet packages (v3.1.5 or newer)

  • Microsoft.Extensions.Configuration
  • Microsoft.Extensions.Configuration.Xml
  • Microsoft.Extensions.Configuration.Binder

Example implementation

var builder = new ConfigurationBuilder();
builder.AddXmlFile("App.config");
var config = builder.Build();

var ccgSettingCcg = config.GetSection("TenantEmailSettings:CcgSettingCcg").Get<CcgSettingCcg>();

Console.WriteLine(ccgSettingCcg.SettingsId);
Console.WriteLine(ccgSettingCcg.TenantId);
Console.WriteLine(ccgSettingCcg.Frequency);

Example for CcgSettingCcg.cs

public class CcgSettingCcg
{
    public string SettingsId { get; set; }
    public string TenantId { get; set; }
    public int? Frequency { get; set; }

    // other properties as required
}

Example App.config

<?xml version="1.0" encoding="utf-8" ?>
<configuration>
  <TenantEmailSettings>
    <CcgSettingCcg SettingsId="520305DC-6E68-4FA3-B53B-18E25E0E21FF" TenantId="A0C55F11-516E-4F7C-9ECD-DEF7C47290B4" Frequency="168" StartDate="03/06/2020" RuleType="Default" To="Ccg" />
  </TenantEmailSettings>
</configuration>

Hope this helps.

As I had to use .NET core 2.2, I had issues with packages, and of course, context can't be inherited. Solution that I had was a bad one but it worked and meet the requirements.

The solution was to treat app.config as it was a general text file and parse from there. Something simple.

Related