azureazure-webjobsappsettingsazure-configuration

Is there a way for a WebJob to read in a custom configuration section, like AppSettings?


I am troubleshooting an Azure WebJob's custom configuration and trying to work out what the expected behaviour should be. I understand that for AppSettings, a webjob will read those from the AppService web.config. But for a custom configuration section, I am not getting that configuration in the web job.

If I add the custom configuration to the webjob app.config, the webjob will read that configuration correctly.

What is the expected behaviour between the webjob app.config and the appservice web.config?


Solution

  • What is the expected behaviour between the webjob app.config and the appservice web.config?

    The expected behavior is that the WebJob will read the AppSettings from the AppService web.config, but for a custom configuration section, it will not read that configuration in the WebJob. If you want to read a custom configuration section in your WebJob, you will need to add it to the WebJob's app.config file.

    I have created a console application in visual studio and then deployed in azure webjobs.

    enter image description here

    Iam able get the hello world output in webjob logs.

    enter image description here

    Then, I tried to add the custom configuration to the webjob app.config as check below.

    <configuration>
        <appSettings>
            <add key="MySetting" value="MyValue" />
        </appSettings>
    </configuration>
    

    Added the below code in program.cs and install Microsoft.Extensions.Configuration in your NuGet packages installer.

    var configuration = new ConfigurationBuilder()
                    .AddJsonFile("appsettings.json", optional: true, reloadOnChange: true)
                    .Build();
    
                var setting1 = configuration["Setting1"];
                var setting2 = configuration["Setting2"];
    
                Console.WriteLine($"Setting1 value: 123 {setting1}");
                Console.WriteLine($"Setting2 value: 432 {setting2}");
    

    Iam able run the app successfully in my local.

    enter image description here

    Then I re-deployed to the webjob and as expected am able run successfully.

    enter image description here

    Here are the logs.

    enter image description here