简体   繁体   中英

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.

"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; }
    }

Try it this way -

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

You missed Settings in your config

"KommuneConfiguration": { 
"Settings" :{
    "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]"
    }
  }},````

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM