4

I am using .NET Core 5.0. I have the below section in appsettings.json file.

"Policies": [
        {
            "name": "ApproverPolicy",
            "description": "ApproverPolicy",
            "rules": [
                {
                    "name": "[email protected]",
                    "description": "[email protected]"
                },
                {
                    "name": "[email protected]",
                    "description": "[email protected]"
                }
            ]
        },
        {
            "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();
2
  • Can you provide json example with all fields for rule? First you can try define Status, UpdatedAt and UpdatedBy as RuleStatus? , DateTime? and string? accordingly Commented Jan 28, 2021 at 8:53
  • 1
    Does this answer your question? Getting value from appsettings.json in .net core Commented Jan 28, 2021 at 8:59

2 Answers 2

6

It's easier to declare a class that matches your appsettings structure. Then you can set them in the start up and inject it into classes using IOptions.

For use in start up

 var appSettingsSection = Configuration.GetSection("Policies");
 services.Configure<List<Policy>>(appSettingsSection);
 var appSettings = appSettingsSection.Get<List<Policy>>();

enter image description here

then when you inject

public SomeClass(IOptions<List<Policy>> options){
     var policies = options.Value;
}

enter image description here

Sign up to request clarification or add additional context in comments.

1 Comment

If you are going to downvote, at least put a comment as to why.
3

You cam use this code.

 var re = _configuration.GetSection("Policies")
           .GetChildren()
           .ToList()
           .Select(x => new Policy
           {
               Name = x.GetValue<string>("name"),
               Description = x.GetValue<string>("description"),
               Rules = x.GetSection("rules").Get<List<Rule>>()
    }).ToList();

Result:

enter image description here

1 Comment

Slightly shorter form based on your response. _configuration.GetSection("Policies").Get<List<Policy>>().ToList();

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.