This is my problem, I have a file app.config
as follows:
<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>
The associated classes are as follows:
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 I read or try as follows:
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;
I have two problems, well, one but it's related: t, s, c, b are null, and they shouldn't be, and two how do I read?
ideas? the file has to be .config but I don't know if I should indicate that it is app.config and if so how to indicate it, so far nothing has worked for me
because the problem could not be solved as it is. What I tried was different nuget packages, good option, but there was none for .net core 2.2 that suited me.
The problem was that I can't just read anywhere in the program, I have to do it in program.cs, outside of that the context is different and therefore it returns null.
Solution: instead of treating app.config as an xml file of type config, I treated it as if it were a normal text file and parsed from there. Something simple, that does not give problems whatever the version of .net