Search code examples
azure-webjobsappsettings

Azure Web Job Console application config setting not getting overridden by Azure App Service Config setting


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

enter image description here

Output from the Web Job

enter image description here


Solution

  • 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): enter image description here

    Output:

    Before adding .AddEnvironmentVariables:

    enter image description here

    After adding .AddEnvironmentVariables:

    enter image description here