0

I have a React SPA with .Net Core WebAPI backend being deployed to Azure App Service. When developing locally i use a appsettings.json file to configure the WebAPI backend. I am setting a key/value pair for an external api called "XyzApi".

{
  "XyzApi": "https://somedomain/api"
}

In a WebAPI controller I access this config setting using the Configuration provider.

var xyzApiUrl = Configuration["XyzApi"]

This works fine.

When I deploy the app to the App Service I need to change the the value of "XyzApi" to override the value from the appsettings.json with a new value. I have added a key/value pair named "XyzApi" in the App Service Application Settings section in Azure.

In the App Service documentation for Application Settings it says the value will be exposed to the configuration as an Environment Variable with the name "APPSETTING_XyzApi". It also says that it is supposed to override the value from the appsettings.json file for the key "XyzApi".

When the app spins up. The issue is that the value for "XyzApi" is still set to the value from the appsettings.json file. It is NOT being overridden. And as expected there is now a new key/value pair for "APPSETTING_XyzApi".

Why is the Azure App Service not overriding the key/value pair from the appsettings.json file with the new value configured in the Azure AppService Application Settings.

Here is the code that configures the Configuration provider.

public class Program
{
    public static void Main(string[] args)
    {
        CreateHostBuilder(args).Build().Run();
    }

    public static IHostBuilder CreateHostBuilder(string[] args) =>
        Host.CreateDefaultBuilder(args)
            .ConfigureWebHostDefaults(webBuilder =>
            {
                webBuilder.UseStartup<Startup>();
            })
            .ConfigureAppConfiguration((hostingContext, config) =>
            {
                var env = hostingContext.HostingEnvironment;

                config.AddJsonFile("appsettings.json", optional: true, reloadOnChange: true)
                    .AddJsonFile($"appsettings.{env.EnvironmentName}.json",
                        optional: true, reloadOnChange: true)
                    .AddEnvironmentVariables(); 
            });
}
Mike Murphy
  • 910
  • 2
  • 11
  • 27

1 Answers1

0

All the steps you did is correct, except the appsettings' name in portal. You add "APPSETTING_XyzApi" in Configuration, which is different with "XyzApi" in appsettings.json. It would override only if the name fit.

Here is an example:

1. I set "myKey": "value1" in appsettings.json in local, And show the value on my index page.

enter image description here

![enter image description here

2. After publish, I set the value to 123123: ![enter image description here

3. The value override:

enter image description here

Doris Lv
  • 3,083
  • 1
  • 5
  • 14
  • Sorry I was not clear, I am correctly setting the key in App Service to "XyxApi". Azure is adding the "APPSETTING_" prefix when exposing the key/value as an Environment Variable. It just does that. Its also supposed to override the value from appsettingd.json but its not. – Mike Murphy Nov 06 '20 at 14:09
  • Actually you got some misunderstanding of this override. The value of appsettings.json would not b override, because this file works locally and when nothing configured in app settings. The value in your hosting web app would be override, just like the testing picture I post.@MikeMurphy – Doris Lv Nov 09 '20 at 01:59