简体   繁体   English

覆盖 azure app 服务应用程序设置中的数组

[英]Override an array in azure app service application settings

In my .NET Core application, I added an array to the appsettings.json, which looks like this:在我的 .NET Core 应用程序中,我向 appsettings.json 添加了一个数组,如下所示:

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

If I would like to override that value from Application Settings in my azure app service, so that it will have empty array:如果我想从我的 azure 应用程序服务中的应用程序设置中覆盖该值,以便它有空数组:

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

Is there a way to do this?有没有办法做到这一点?

I tried to put我试着把

SettingsA:PropsA  ->  []

In the application settings, but it doesn't seem to override the value of appsettings.json在应用程序设置中,但似乎没有覆盖 appsettings.json 的值

The answer here https://www.ryansouthgate.com/2016/03/23/iconfiguration-in-netcore/ is that you can override elements within an array or add additional elements but he says that you can't override the entire array, which seems strange.这里的答案https://www.ryansouthgate.com/2016/03/23/iconfiguration-in-netcore/是您可以覆盖数组中的元素或添加其他元素,但他说您不能覆盖整个数组,这看起来很奇怪。

Syntax for overriding uses zero-based access such as SettingsA:PropA:0:Something and I have tried this on App Services and can confirm it works.覆盖语法使用从零开始的访问,例如 SettingsA:PropA:0:Something,我已经在 App Services 上尝试过,可以确认它有效。

Just to top up all the great answers already given here, here is how we did it in our real life scenario.为了补充这里已经给出的所有优秀答案,以下是我们在现实生活场景中的做法。

We needed a collection of supported languages to be configured in our app settings.我们需要在我们的应用程序设置中配置一组受支持的语言。 Here is how it looked in the appSettings.json in our .NET Core project:下面是它在我们的 .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)"
        }
    ]
}

And this is how it ended up looking in our Azure App Service:这就是它最终在我们的 Azure 应用服务中的样子:

在此处输入图片说明

It's a bit fiddly, especially if you have a larger or more complex hierarchy, but I don't think there's another way for now.这有点繁琐,特别是如果您有更大或更复杂的层次结构,但我认为目前没有其他方法。

You could use AddEnvironmentVariables property to achieve override appsettings on azure to local settings.您可以使用AddEnvironmentVariables属性将azure 上的AddEnvironmentVariables 覆盖到本地设置。

First configure the setting in portal:首先在门户中配置设置: 在此处输入图片说明

Note : The value here is null.注意:这里的值为空。

To override nested keys in the App Settings section we can define a variable using the full path SettingsA:PropA as name or using double underscore SettingsA__PropA .要覆盖 App Settings 部分中的嵌套键,我们可以使用完整路径SettingsA:PropA作为名称或使用双下划线SettingsA__PropA来定义变量。 You could refer to this article .你可以参考这篇文章

In local, you could configure as below: In Startup.cs:在本地,您可以配置如下: 在 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"));

        }

In appsettings.json:在 appsettings.json 中:

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

In 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();
        }

In SettingsOption:在设置选项中:

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

After you publish the project to azure, it will override the PropA value.将项目发布到 azure 后,它将覆盖 PropA 值。 For more details about how to read appsetting from asp.net core, please follow this case .有关如何从 asp.net core 读取 appsetting 的更多详细信息,请关注此案例

As far as I know, the settings in the Application settings are injected into your appsettings.json at runtime , overriding existing settings.据我所知, 应用程序设置中的设置在运行时注入到您的appsettings.json,覆盖现有设置。 It will not change the appsettings.json file, because Azure GUI (Connection trings, Application settings) uses environment variables internally.它不会更改 appsettings.json 文件,因为 Azure GUI(连接字符串、应用程序设置)在内部使用环境变量。

Here is a similar post for you to refer.这里有一个类似的帖子供您参考。 You could override the settings during release via VSTS.您可以在发布期间通过 VSTS 覆盖设置。 Here are the links to Colin's ALM Corner Build & Release Tools and tutorial .这里是Colin 的 ALM Corner Build & Release Tools教程的链接 For more details, you could refer to psycho's reply in the above post.更多详情,你可以参考上面帖子中psycho的回复。

If there is a need for an appsettings.json's value to be overwritten during VSTS release activity (before it will be published to Azure), Colin's ALM Corner Build & Release Tools can be used.如果需要在 VSTS 发布活动期间(在发布到 Azure 之前)覆盖 appsettings.json 的值,可以使用 Colin 的 ALM Corner Build & Release Tools。

This is very strange behaviour by microsoft team.这是微软团队非常奇怪的行为。 Looks like they are not aware of how the reality looks like.看起来他们不知道现实是什么样子。

I ended up with having a string with comma separated values instead of arrays.我最终得到了一个用逗号分隔的值而不是数组的字符串。

To to this using a multiplataform syntax on Azure use __为此,在 Azure 上使用多平台语法使用 __

This way works for both Windows and Linux deployed App Services:这种方式适用于 Windows 和 Linux 部署的应用服务:

SettingsA__PropA__0__Something

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

相关问题 使用Azure App Service的应用程序设置导致HTTP 500错误 - Using Application settings of Azure App Service resulting into an HTTP 500 error Azure webjobs不会使用Azure应用程序设置覆盖appsettings.json - Azure webjobs does not override appsettings.json with Azure Application Settings c# .Net Core 3.1 Azure App Service Windows - 应用程序无法检索应用程序设置 - c# .Net Core 3.1 Azure App Service Windows - Application not able to retrieve App settings HTTPModule 部分的 Azure 应用服务配置设置 - Azure App Service Configuration Settings for HTTPModule section 如何为 ASP.NET Core 3.1 配置 Azure App Service 应用程序设置? - How to configure Azure App Service application settings for ASP.NET Core 3.1? Azure App Service中的ASP.NET应用程序设置如何工作? - How do ASP.NET application settings in an Azure App Service work? 在 Azure Function 应用程序设置中使用数组 - Using Array in Azure Function Application Settings 在 Azure web 应用程序设置中使用数组 - Using an array in Azure web app settings 使用 KeyVault 机密覆盖 Azure 应用服务中和本地的应用设置 - Using KeyVault secrets to override appsettings in Azure App Service and locally Azure应用服务在应用程序之前终止https? - Azure App Service terminating https before application?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM