简体   繁体   English

如何使用值数组读取appsettings.json

[英]How to read appsettings.json with array of values

I have the following appSettings.json file: 我有以下appSettings.json文件:

  "SundrySettings": {

    "CookieName": "Cookie",

    "AccessGroup": "Software Development",

    "Terminals" :  {
      "Raucherplatz" : "tablet1.local",
      "Service" :      "tablet2.local",  
      "Technik" :      "tablet3.local",  
      "Technik" :      "tablet4.local",  
      "Container" :    "tablet5.local"
    }
  }
}

That I would like to load into the following structure: 我想加载到以下结构中:


    public class Terminal
    {
        public string Name;
        public string Description;
    }

    public class SundryOptions
    {
        public string CookieName { get; set; } = "dummy";
        public string HRAccessGroup { get; set; } = "dummy";
        public List<Terminal> Terminals;
    }

that I would try to load using the following commands: 我将尝试使用以下命令进行加载:

ServiceProvider sp = services.BuildServiceProvider();
SundryOptions sundryOptions = sp.GetService<IOptions<SundryOptions>>().Value;

The problem I have is that using Property Initialisers never sets the Terminals List correctly. 我的问题是使用属性初始化程序永远不会正确设置终端列表。 I do need a list (and not a Dictionary) as the enties could be double ie Technik in my example. 我确实需要一个列表(而不是字典),因为实体可能是双重的,即在我的示例中为Technik。

I am assuming that I have some error in the Class -> I would be happy for any pointers. 我假设我在类中有一些错误->对于任何指针我都会很高兴。

Implement processing of configuration as following somewhere approporiate like this: 像这样在适当的地方执行配置处理:

var cookieName = 
Configuration.GetSection("SundrySettings:CookieName").Value;
var accessGroup = Configuration.GetSection("SundrySettings:AccessGroup").Value;

var terminals = new List<Terminal>()

var terminalSections = this.Configuration.GetSection("Terminals").GetChildren();
foreach (var item in terminalSections)
{
    terminals.Add(new Terminal 
    {
           // perform type mapping here 
    });
}

SundryOptions sundryOption = new SundryOptions()
{
        CookieName = cookieName,
        HRAccessGroup = accessGroup,
        Terminals = terminalList
};

Of course there could be shorter version, but you can start from here. 当然可能会有较短的版本,但是您可以从这里开始。

Do as follows: 进行如下操作:

var cookieName = Configuration.GetSection("SundrySettings:CookieName").Value;
var accessGroup = Configuration.GetSection("SundrySettings:AccessGroup").Value;
var terminals = Configuration.GetSection("SundrySettings:Terminals").GetChildren();

List<Terminal>  terminalList = new List<Terminal>();

foreach (var keyValuePair in terminals)
{
     Terminal termial = new Terminal()
     {
          Name = keyValuePair.Key,
          Description = keyValuePair.Value
     };

     terminalList.Add(termial);
}

SundryOptions sundryOption = new SundryOptions()
{
            CookieName = cookieName,
            HRAccessGroup = accessGroup,
            Terminals = terminalList
};

I have checked with the exact configuration you provided and it works perfectly. 我已经检查了您提供的确切配置,并且效果很好。

If Terminals is a list, in your appSettings, it should be an array, not an object. 如果Terminals是一个列表,则在您的appSettings中,它应该是一个数组,而不是对象。

  "Terminals" :  [
  "Raucherplatz" : "tablet1.local",
  "Service" :      "tablet2.local",  
  "Technik" :      "tablet3.local",  
  "Technik" :      "tablet4.local",  
  "Container" :    "tablet5.local"
]

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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