I'm running a Dot Net 6.0 Console application as a web job in Azure App Service. I want to override the configuration setting present in apsettings.json file by the one present in Azure App Service Configuration.
This is the code present in the console application to read the configuration
static async Task Main(string[] args)
{
var configuration = new ConfigurationBuilder().SetBasePath(Directory.GetCurrentDirectory())
.AddJsonFile("appsettings.json").Build();
var testKey = configuration.GetSection($"TestKey").Value;
Console.WriteLine($"TestKey {testKey}");
And this is apsettings.json file
{ "TestKey": "TestValue" }
Application setting in the Azure App Service where console app is deployed as a Web Job
Output from the Web Job
AFAIK, To override the configuration setting in apsettings.json file, add .AddEnvironmentVariables in Program.cs.
I have reproduced in my environment and got the expected results.
Program.cs
var config = new ConfigurationBuilder()
.SetBasePath(AppDomain.CurrentDomain.BaseDirectory)
.AddJsonFile("appsettings.json")
.AddEnvironmentVariables()
.Build();
var testKey = config.GetSection($"TestName").Value;
Console.WriteLine($"TestKey {testKey}");
appsettings.json
{
"TestName": "TestValue"
}
Application settings in Azure App Service(Portal):
Output:
Before adding .AddEnvironmentVariables:
After adding .AddEnvironmentVariables: