Mike Murphy
Mike Murphy

Reputation: 940

Azure App Service Application Settings not overriding appsettings.json values

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

Upvotes: 0

Views: 3257

Answers (1)

Doris Lv
Doris Lv

Reputation: 3398

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

Upvotes: 0

Related Questions