user1368182
user1368182

Reputation: 473

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

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

Upvotes: 5

Views: 7032

Answers (2)

N-ate
N-ate

Reputation: 6926

Windows and Linux containers are a bit different.

For the following settings:

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

Windows key: AppSettings:Hello

Linux key: AppSettings__Hello

Upvotes: 7

Joey Cai
Joey Cai

Reputation: 20067

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

Upvotes: 6

Related Questions