0

I am building a C# .NET 5.0 application to talk to an external API for which we have multiple accounts that we can post data to. I am trying to store these details in appsettings.json file and use the options pattern to retrieve the settings.

I have the following structure in my app settings.json

"SomeApi": {
    "Url": "https://api.someapi.net/v3/",
    "Secret": "mysecretvlaue",
    "ClientId": "myclientid",
    "Accounts": [
      {
          "Name": "Bob",
          "Username": "HisUsername",
          "Password": "HisPassword",
          "PostingLocation": "HisLocation"
      },
      {
          "Name": "Fred",
          "Username": "HisUsername",
          "Password": "HisPassword",
          "PostingLocation": "HisLocation"
      }
    ]
  },

I have a class set up like this:

public class SomeApiOptions
    {
        public const string SomeApi = "SomeApi";
   
        public string Url { get; set; }
        public string Secret { get; set; }
        public string ClientId { get; set; }

        public List<Account> Accounts { get; set; }
    }
    public class Account
    {
        string Name { get; set; }
        string Username { get; set; }
        string Password { get; set; }
        string PostingLocation { get; set; }
    }

My startup.cs has this:

services.Configure<SomeApiOptions>(Configuration.GetSection(SomeApiOptions.SomeApi));

Finally, I inject IOptions to the class where I need to use the settings like so:

Public MyClass (IOptionsSnapshot<SomeApiOptions> options)

The issue I am having is the Accounts never get populated with their respective values. Url, Secret etc. are populated and the number of elements items in the Accounts list is correct, but all of the properties have null values.

I have looked at similar questions and tried restructuring my JSON a few ways but still end up with null values.

Any help greatly appreciated.

2
  • I see a couple of problems here. First, none of the properties in the Account class are public so you need to change that. Second, the Location property doesn't match the JSON which is PostingLocation. Commented May 7, 2021 at 9:18
  • In JSON there is property called PostingLocation while its name in object is Location try to make them same or... add data Annotation to your object Commented May 7, 2021 at 9:18

1 Answer 1

2

I see a couple of problems here.

First, none of the properties in the Account class are public so you need to change that:

Second, the Location property doesn't match the JSON which is PostingLocation. Your class should be:

public class Account
{
    public string Name { get; set; }
    public string Username { get; set; }
    public string Password { get; set; }
    public string PostingLocation { get; set; }
}
Sign up to request clarification or add additional context in comments.

3 Comments

Apologies, that was a typo, I have edited the post to reflect the PostingLocation is as you describe.
The properties still need to be public though, that's your real problem.
Thanks! I can't believe I missed that. Public properties solved the issue immediately.

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.