gpt4 book ai didi

Azure 应用服务 - 数组中的应用设置数组

转载 作者:行者123 更新时间:2023-12-03 07:08:25 26 4
gpt4 key购买 nike

我目前正在尝试在 Azure 应用服务配置中创建“复杂”的应用程序设置结构,但失败了。

我的 appsettings.json 看起来很糟糕。像这样:

{
"AgentPools": [
{
"Id": "1",
"Name": "PoolName1",
"AllowedProjects": []
},
{
"Id": "2",
"Name": "PoolName2",
"AllowedProjects": [
"myproject1",
"myproject2"
]
}
]
}

我现在的问题是,我在数组中该数组的设置中似乎做错了。

配置与 Azure 应用服务 (Linux) 中的配置类似:

  • AgentPools__0__Id -> 1 => 工作
  • AgentPools__0__Name -> PoolName1 => 工作
<小时/>
  • AgentPools__1__Id -> 2 => 工作
  • AgentPools__1__Name -> Poolname2 => 工作
  • AgentPools__1__AllowedProjects__0 -> myproject1 => 不工作
  • AgentPools__1__AllowedProjects__1 -> myproject2 => 不工作
<小时/>

我真的做错了什么吗,或者甚至不可能构建一个更复杂的结构?

我正在 netcore 中编程。

最佳答案

  1. 可以使用the tool (require .NET SDK)this answer帮助转换为多种格式的环境应用程序设置(Azure 应用程序服务/配置、docker-compose)。

  2. 转换为环境后,您的 appsettings.json 将如下所示。那么看来您从配置中检索值的方式可能不正确。

AgentPools__0__Id
1

AgentPools__0__Name
PoolName1

AgentPools__1__AllowedProjects__0
myproject1

AgentPools__1__AllowedProjects__1
myproject2

AgentPools__1__Id
2

AgentPools__1__Name
PoolName2
  • 提取配置的简单方法是定义类配置并将其绑定(bind)为 herehere 。配置绑定(bind)数组可以是数组或列表等
  • internal class AgentPoolsConfig
    {
    public int ID { get; set; }
    public string? Name { get; set; }
    public string[] AllowedProjects { get; set; } = Array.Empty<string>();
    }

    internal class AppConfig
    {
    public AgentPoolsConfig[] AgentPools { get; set; } = Array.Empty<AgentPoolsConfig>();
    }

    var agentPools = configuration.GetSection("AgentPools").Get<AgentPoolsConfig[]>() ?? Array.Empty<AgentPoolsConfig>();

    var appConfig = configuration.Get<AppConfig>();
  • 可以使用 launchSettings.json 进行环境调试
  • {
    "profiles": {
    "ConsoleApp1": {
    "commandName": "Project",
    "environmentVariables": {
    "AgentPools__1__Id": "2",
    "AgentPools__0__Id": "1",
    "AgentPools__1__Name": "PoolName2",
    "AgentPools__0__Name": "PoolName1",
    "AgentPools__1__AllowedProjects__0": "myproject1",
    "AgentPools__1__AllowedProjects__1": "myproject2"
    }
    }
    }
    }
  • 如何使用该工具
  • how to use the tool

    关于Azure 应用服务 - 数组中的应用设置数组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/70982723/

    26 4 0
    Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
    广告合作:1813099741@qq.com 6ren.com