I have a WebAPI that I am publishing it on Azure. I am using .Net Core 2.0 on my application.
I have a file called appsettings.json which has the following configuration:
{
"Logging": {
"IncludeScopes": false,
"Debug": {
"LogLevel": {
"Default": "Warning"
}
},
"Console": {
"LogLevel": {
"Default": "Warning"
}
}
},
"AppConfiguration": {
"MyVariable": false,
}
...
}
In my application, running in localhost, I can get the value from "MyVariable".
When I publish it to Azure, I can also get the value from "MyVariable".
However, when I go to the Application Settings of my Azure application, I set "MyVariable" there to "true", but my application keeps getting the value "false".
To summarise, I am not able to get the value from Azure, only from the appsettings.json.
I tried using, on Azure, the following key-value:
AppConfiguration:MyVariable - true
MyVariable - true
None of them worked.
Can anyone help me in how to get the value from the Azure Application Settings?
Edit 1 My program class:
public static IWebHost BuildWebHost(string[] args) =>
WebHost.CreateDefaultBuilder(args)
.ConfigureAppConfiguration(
(WebHostBuilderContext context, IConfigurationBuilder builder) =>
{
builder.Sources.Clear();
builder
.AddEnvironmentVariables()
.AddJsonFile("appsettings.json", optional: false, reloadOnChange: true);
})
.UseApplicationInsights()
.UseStartup<Startup>()
.Build();
The order of the settings sources matters. Change this:
builder
.AddEnvironmentVariables()
.AddJsonFile("appsettings.json", optional: false, reloadOnChange: true);
to this:
builder
.AddJsonFile("appsettings.json", optional: false, reloadOnChange: true)
.AddEnvironmentVariables();
to have the settings from the Azure environment override the ones in the settings file.
In Net Core 2.o or superior, the order matters. if you check here you can see the order:
A typical sequence of configuration providers is:
- Files (appsettings.json, appsettings..json, where is the app's current hosting environment)
- User secrets (Secret Manager) (in the Development environment only)
- Environment variables
- Command-line arguments
So, you must change .AddEnvironmentVariables() to this:
builder
.AddJsonFile("appsettings.json", optional: false, reloadOnChange: true)
.AddEnvironmentVariables();
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With