I am using .NET Core 5.0. I have the below section in appsettings.json file.
"Policies": [
{
"name": "ApproverPolicy",
"description": "ApproverPolicy",
"rules": [
{
"name": "abc@gmail.com",
"description": "abc@gmail.com"
},
{
"name": "def@gmail.com",
"description": "def@gmail.com"
}
]
},
{
"name": "RegionPolicy",
"description": "RegionPolicy",
"rules": [
{
"name": "East US",
"description": "East US"
},
{
"name": "North Europe",
"description": "North Europe"
}
]
}
]
I have the below C# classes defined.
public class Policy : PolicyDefinition
{
public IList<Rule> Rules { get; set; }
}
public class PolicyDefinition
{
public string Name { get; set; }
public string Description { get; set; }
}
public class Rule
{
public string Name { get; set; }
public string Description { get; set; }
public RuleStatus Status { get; set; }
public DateTime UpdatedAt { get; set; }
public string UpdatedBy { get; set; }
}
public enum RuleStatus
{
Undefined = 0,
Blocked = 1,
Approved = 2,
Rejected = 4,
Unknown = 8
}
Iam using the below code to read the Policies section from appsettings.json file. Name and Description properties are populated as expected but "Rules" property is NULL. What changes are required to populated Rules property properly? Your help is very much appreciated.
_configuration.GetSection("Policies")
.GetChildren()
.ToList()
.Select(x => new Policy
{
Name = x.GetValue<string>("name"),
Description = x.GetValue<string>("description"),
Rules = x.GetValue<IList<Rule>>("rules")
}).ToList();
The following code worked in this scenario. Thanks for all the help.
_configuration.GetSection("Policies").Get<List<Policy>>().ToList();