Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

How to load settings dictionary from appsettings.json?

I have some database settings in my appsettings.json file. This system connects to different databases in production there are 60+ of them.

Each customer has an internal number in our system and their database has a connection string.

What I am trying to do is load these settings, so that I can validate that they are working at startup / health check. I have a method which is called from ConfigureServices. It takes IConfiguration configuration as a parameter.

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

"KommuneConfiguration": {
    "localhost": {
      "KommuneNumber": "localhost",
      "ConnectionString": "Server=H52371;Database=DeliveryReport;Trusted_Connection=True;MultipleActiveResultSets=true",
      "TableName": "[DeliveryReport].[dbo].[DeliveryReport]"
    },
    "000": {
      "KommuneNumber": "000",
      "ConnectionString": "Server=172.31.112.106;Database=000_XXXX_02_EDW;Trusted_Connection=True;MultipleActiveResultSets=true",
      "TableName": "[000_XXXX_02_EDW].[gateway].[DeliveryReport]"
    },
    "550": {
      "KommuneNumber": "550",
      "ConnectionString": "Server=172.31.112.106;Database=550_XXXX_02_EDW;Trusted_Connection=True;MultipleActiveResultSets=true",
      "TableName": "[550_XXXX_02_EDW].[gateway].[DeliveryReport]"
    },
    "607": {
      "KommuneNumber": "607",
      "ConnectionString": "Server=172.31.112.106;Database=607_XXXX_02_EDW;Trusted_Connection=True;MultipleActiveResultSets=true",
      "TableName": "[607_XXXX_02_EDW].[gateway].[DeliveryReport]"
    }
  },

what I have tried.

This seams to load the first level IE they key but the object value is null

var serviceClientSettingsConfigKommune = configuration.GetSection("KommuneConfiguration").GetChildren()
            .ToDictionary(x => x.Key, x => x.Value); 

This just loads null

  var hold = new KommuneSettings();
  configuration.GetSection("KommuneConfiguration").Bind(hold);

This does the same as the first one did it loads the key but the value is null.

var serviceClientSettingsConfigKommune = configuration.GetSection("KommuneConfiguration").Get<Dictionary<string, KommuneConfiguration>>(); 

These are the objects I have been testing with.

public class KommuneConfiguration
    {
        public string KommuneNumber { get; set; }
        public string TableName { get; set; }
        public string ConnectionString { get; set; }
    }

 public class KommuneSettings
    {
        public Dictionary<string,KommuneConfiguration> Settings { get; set; }
    }

>Solution :

Try it this way –

var serviceClientSettingsConfigKommune = configuration.GetSection("KommuneConfiguration").Get<List<KommuneConfiguration>>();
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading