【问题标题】:Override an array in azure app service application settings覆盖 azure app 服务应用程序设置中的数组
【发布时间】:2018-10-16 21:31:59
【问题描述】:

在我的 .NET Core 应用程序中,我向 appsettings.json 添加了一个数组,如下所示:

{
  "SettingsA": {
    "PropA": [
        "ChildObjectA": {
          ...
        },
        "ChildObjectB": {
          ...
        }
    ]
  }
}

如果我想从我的 azure 应用服务中的应用程序设置中覆盖该值,使其具有空数组:

{
  "SettingsA": {
    "PropA": []
  }
}

有没有办法做到这一点?

我试着放了

SettingsA:PropsA  ->  []

在应用设置中,但是好像没有覆盖appsettings.json的值

【问题讨论】:

    标签: c# azure asp.net-core azure-web-app-service


    【解决方案1】:

    这是微软团队非常奇怪的行为。看起来他们不知道现实的样子。

    我最终得到了一个带有逗号分隔值的字符串而不是数组。

    【讨论】:

      【解决方案2】:

      为了补充这里已经给出的所有很好的答案,以下是我们在现实生活场景中的做法。

      我们需要在应用设置中配置一组受支持的语言。下面是它在我们的 .NET Core 项目中的 appSettings.json 中的样子:

      {
          ...
      
          "SupportedLanguages": [
              {
                  "Code": "en-AU",
                  "Name": "English (Australia)"
              },
              {
                  "Code": "en-GB",
                  "Name": "English (United Kingdom)"
              },
              {
                  "Code": "en-US",
                  "Name": "English (United States)"
              }
          ]
      }
      

      这就是它最终在我们的 Azure 应用服务中的样子:

      这有点繁琐,尤其是如果您有更大或更复杂的层次结构,但我认为目前没有其他方法。

      【讨论】:

        【解决方案3】:

        https://www.ryansouthgate.com/2016/03/23/iconfiguration-in-netcore/ 的答案是你可以覆盖数组中的元素或添加其他元素,但他说你不能覆盖整个数组,这看起来很奇怪。

        用于覆盖的语法使用从零开始的访问,例如 SettingsA:PropA:0:Something,我已经在 App Services 上尝试过,并且可以确认它有效。

        【讨论】:

        • 这是唯一相关的答案,而且是正确的。简短的回答是应用程序设置中的以下语法:ArrayName:Index Value。来自问题: SettingsA:PropA:0:ChildObjectA value
        【解决方案4】:

        您可以使用AddEnvironmentVariables 属性来实现override appsettings on azure 到本地设置。

        首先在门户中配置设置:

        注意:这里的值为空。

        要覆盖应用设置部分中的嵌套键,我们可以使用完整路径 SettingsA:PropA 作为名称或使用双下划线 SettingsA__PropA 定义一个变量。你可以参考这个article

        在本地,您可以配置如下: 在 Startup.cs 中:

        public Startup(IHostingEnvironment env)
                {
                    var builder = new ConfigurationBuilder()
                        .SetBasePath(env.ContentRootPath)
                        .AddJsonFile("appsettings.json", optional: false, reloadOnChange: true)
                        .AddJsonFile($"appsettings.{env.EnvironmentName}.json", optional: true)
                        .AddEnvironmentVariables();
                    configuration = builder.Build();
                }
                public IConfiguration configuration { get; }
        
                // This method gets called by the runtime. Use this method to add services to the container.
                public void ConfigureServices(IServiceCollection services)
                {
                    services.AddMvc();
                    services.AddOptions();
                    services.Configure<SettingsOptions>(configuration.GetSection("SettingsA"));
        
                }
        

        在 appsettings.json 中:

        {"SettingsA": {
            "PropA": ["a","b"]
            }
        }
        

        在 HomeController 中:

        private readonly IOptions<SettingsOptions> options;
                public HomeController(IOptions<SettingsOptions> options)
                {
                    this.options = options;
                }
        
                public IActionResult Index()
                {
                    var value = options.Value;
                    ViewBag.Index = value.PropA+"success";
                    return View();
                }
        

        在设置选项中:

        public class SettingsOptions
            {
                public string SettingsA { get; set; }
                public string PropA { get; set; }
            }
        

        将项目发布到 azure 后,它将覆盖 PropA 值。 有关如何从 asp.net core 读取 appsetting 的更多详细信息,请关注此case

        【讨论】:

        • 这根本不能回答问题。这个问题专门针对数组。
        【解决方案5】:

        据我所知,Application settings 中的设置会在运行时 注入到您的appsettings.json 中,从而覆盖现有设置。 它不会更改 appsettings.json 文件,因为 Azure GUI(连接字符串、应用程序设置)在内部使用环境变量。

        这里有一个similar post 供您参考。您可以在发布期间通过 VSTS 覆盖设置。这是Colin's ALM Corner Build & Release Toolstutorial 的链接。更详细的可以参考上面帖子中psycho的回复。

        如果需要在 VSTS 发布活动期间(在将其发布到 Azure 之前)覆盖 appsettings.json 的值,可以使用 Colin 的 ALM Corner Build & Release Tools。

        【讨论】:

        • 感谢您的澄清。我正是这个意思。我的意思是override 不是替换
        • 这不适用于 webjobs
        猜你喜欢
        • 1970-01-01
        • 2016-06-22
        • 1970-01-01
        • 1970-01-01
        • 2020-09-05
        • 1970-01-01
        • 2018-10-28
        • 1970-01-01
        • 2017-12-31
        相关资源
        最近更新 更多