Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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:

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

like image 217
muhihsan Avatar asked May 07 '18 06:05

muhihsan


3 Answers

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.

like image 168
KenR Avatar answered Oct 19 '22 23:10

KenR


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.

like image 32
Lukos Avatar answered Oct 19 '22 23:10

Lukos


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.

like image 31
Joey Cai Avatar answered Oct 20 '22 01:10

Joey Cai