Azure app service app settings in the portal are not overriding appsettings.json values
Asked Answered
H

2

5

I have the following in Startup (asp.net core 2.2 proj):

 public Startup(IConfiguration configuration, IHostingEnvironment environment)
 {
        Configuration = configuration;
        Environment = environment;

        new ConfigurationBuilder()
            .SetBasePath(Environment.ContentRootPath)
            .AddJsonFile("appsettings.json", optional: true)
            .AddEnvironmentVariables()
            .Build();
 }

 public void ConfigureServices(IServiceCollection services)
 {
        var appSettings = new AppSettings();

        Configuration.Bind("appSettings", appSettings);

        services.AddSingleton(appSettings);

        ....
}

I've set values to override all my app setting values in appsettings.json, but my app service is still using what is in appsettings instead of what I put in the portal. Per the documentation, those portal app settings for the app service should override the appsettings.json file and get used instead. Am I missing a step?

Thanks

Edit:

Even changing Startup to the following doesn't pick up my azure app settings:

    public Startup(IConfiguration configuration, IHostingEnvironment environment)
    {
        Configuration = configuration;
        Environment = environment;

        Configuration = new ConfigurationBuilder()
            .AddConfiguration(configuration)
            .SetBasePath(Environment.ContentRootPath)
            .AddJsonFile("appsettings.json", optional: true)
            .AddEnvironmentVariables()
            .Build();
    }
Hays answered 12/12, 2019 at 20:45 Comment(3)
You're not assigning the result of your instantiation and construction of ConfigurationBuilder.Leotie
What do I assign it to? The Configuration property? ...instead of the IConfiguration object that gets passed in? Thanks!Hays
I changed it, please see my edit above, but that doesn't seem to do anything....it's still not loading my app settings from the azure app service.Hays
B
6

When you want to get Appsettings on portal you need to use like AppSettings:Hello to reference variable names inside complex structures in appsettings.*.json files. Refer to this article. The following is steps you could refer.

HomeController:

private AppSettings AppSettings { get; set; }
public HomeController(IOptions<AppSettings> appSettings)
{
    AppSettings = appSettings.Value;
}
public IActionResult Index()
{
    ViewBag.Message = AppSettings.Hello;
    return View();
}

AppSettings.cs:

public class AppSettings
{
    public string Hello { get; set; }
}

Startup.cs:

public Startup(IConfiguration configuration, IHostingEnvironment environment)
{
    Configuration = configuration;
    Configuration = new ConfigurationBuilder()
        .AddConfiguration(configuration)
        .SetBasePath(environment.ContentRootPath)
        .AddJsonFile("appsettings.json", optional: false, reloadOnChange: true)
        .AddJsonFile($"appsettings.{environment.EnvironmentName}.json", optional: true)
        .AddEnvironmentVariables()
        .Build();
}
public void ConfigureServices(IServiceCollection services)
{
    services.Configure<AppSettings>(Configuration.GetSection("AppSettings"));
}

appsettings.json:

"AppSettings": {
    "Hello": "world"
}

On portal:

enter image description here

And the output:

enter image description here

Benthos answered 13/12, 2019 at 3:9 Comment(1)
So really just adding "Appsettings:" before my settings name is what fixed it. I did add the .AddJsonFile($"appsettings.{environment.EnvironmentName}.json", optional: true) call anyway after that just to make sure that's in there. Thanks!Hays
L
7

Windows and Linux containers are a bit different.

For the following settings:

"AppSettings": {
    "Hello": "world"
}

Windows key: AppSettings:Hello

Linux key: AppSettings__Hello

Lytic answered 24/5, 2021 at 22:5 Comment(2)
Does it have to be named AppSettings as the root of the appsettings.json? If I use a custom name like "WeatherApi": { "Hello": "world" } and then in App Service Configuration use WeatherApi__Hello it doesn't override the value. But having AppSettings name bot in appsettings.json and using AppSettings__Hello seems to work.Backsword
You can make as many files of whatever name you like and add them to the configuration at startup. I'm not sure if that is what you're asking.Lytic
B
6

When you want to get Appsettings on portal you need to use like AppSettings:Hello to reference variable names inside complex structures in appsettings.*.json files. Refer to this article. The following is steps you could refer.

HomeController:

private AppSettings AppSettings { get; set; }
public HomeController(IOptions<AppSettings> appSettings)
{
    AppSettings = appSettings.Value;
}
public IActionResult Index()
{
    ViewBag.Message = AppSettings.Hello;
    return View();
}

AppSettings.cs:

public class AppSettings
{
    public string Hello { get; set; }
}

Startup.cs:

public Startup(IConfiguration configuration, IHostingEnvironment environment)
{
    Configuration = configuration;
    Configuration = new ConfigurationBuilder()
        .AddConfiguration(configuration)
        .SetBasePath(environment.ContentRootPath)
        .AddJsonFile("appsettings.json", optional: false, reloadOnChange: true)
        .AddJsonFile($"appsettings.{environment.EnvironmentName}.json", optional: true)
        .AddEnvironmentVariables()
        .Build();
}
public void ConfigureServices(IServiceCollection services)
{
    services.Configure<AppSettings>(Configuration.GetSection("AppSettings"));
}

appsettings.json:

"AppSettings": {
    "Hello": "world"
}

On portal:

enter image description here

And the output:

enter image description here

Benthos answered 13/12, 2019 at 3:9 Comment(1)
So really just adding "Appsettings:" before my settings name is what fixed it. I did add the .AddJsonFile($"appsettings.{environment.EnvironmentName}.json", optional: true) call anyway after that just to make sure that's in there. Thanks!Hays

© 2022 - 2024 — McMap. All rights reserved.