6
votes

In my .NET Core application, I added an array to the appsettings.json, which looks like this:

{
  "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:

{
  "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

5

5 Answers

6
votes

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.

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.

6
votes

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:

{
    ...

    "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:

enter image description here

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.

2
votes

You could use AddEnvironmentVariables property to achieve override appsettings on azure to local settings.

First configure the setting in portal: enter image description here

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. You could refer to this article.

In local, you could configure as below: In 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:

{"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. For more details about how to read appsetting from asp.net core, please follow this case.

0
votes

As far as I know, the settings in the Application settings are injected into your appsettings.json at runtime, overriding existing settings. It will not change the appsettings.json file, because Azure GUI (Connection trings, Application settings) uses environment variables internally.

Here is a similar post for you to refer. You could override the settings during release via VSTS. Here are the links to Colin's ALM Corner Build & Release Tools and tutorial. For more details, you could refer to psycho's reply in the above post.

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.

0
votes

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.