0
votes

When WebJob starts (Program.Main()), configuration item in Azure Application Settings does NOT override the value set in app.config.

Both ConfigurationManager.AppSettings and CloudConfigurationManager.GetSetting return the same value from app.config instead of the override in Azure Application Settings

While once WebJob Function gets registered, settings in Azure become accessible as normal when it's fired

Environment: VS2017 v15.8.5,
Target framework .NET Framework 4.6,
Microsoft.Azure.WebJobs v2.3.0,
Microsoft.Azure.WebJobs.Core v2.3.0,
Microsoft.Azure.WebJobs.Extensions v2.2.0,
Microsoft.Web.WebJobs.Publish v2.0.0
Microsoft.Azure.ConfigurationManager v4.0.0

1.App.config:

<appSettings>
  <add key="EXECUTION_ENVIRONMENT" value="PleaseSpecify" />
</appSettings>

2.Azure Application Settings:

APP SETTING NAME VALUE

EXECUTION_ENVIRONMENT UAT

3.Program.cs:

class Program
{

    static void Main(string[] args)

    {

         // return "PleaseSpecify" instead of "UAT" in Azure

         var ee1 = ConfigurationManager.AppSettings["EXECUTION_ENVIRONMENT"];

         Console.WriteLine(ee1);


        // return "PleaseSpecify" instead of "UAT" in Azure

        var ee2 = 
Microsoft.Azure.CloudConfigurationManager.GetSetting("EXECUTION_ENVIRONMENT");

        Console.WriteLine(ee2);

    }

}

4.Publish to Azure through Visual Studio...

5.Check WebJob logs will see the problem as commented in the source above

I would hope settings in Azure take precedence over those in App.config but they're not!

Any thoughts are much appreciated!

2
Did you try to set a different value and test it? Are you sure you could read the settings?George Chen
You could go to this folder in Kudu D:\local\Temp\jobs\continuous check ***.exe.config file if settings are injected to your configuration. You could try to use this to get variable. Environment.GetEnvironmentVariable("environment")George Chen
George, thank you for the great inputs! The problem seems to be caused by webjob deployment. our App.config use <appSettings file=...> to reference to CommonSettings.config shared by a few projects and "EXECUTION_ENVIRONMENT" is part of CommonSettings.configCalvin Zhai
It's clear not that after publishing webjob, Azure Application Settings are injected into webjob exe.config so entry <add key="EXECUTION_ENVIRONMENT" value="UAT" /> gets added into .exe.config which is good, but the problem is the same setting in CommonSettings.config does NOT gets override by Azure settings, thus during runtime, ConfigurationManager will simply return the value in CommonSettings.config instead of the updated one in webjob .exe.configCalvin Zhai
In addition, Environment.GetEnvironmentVariable("EXECUTION_ENVIRONMENT") does return "UAT" as expected but our codes are simply using ConfigurationManager everywhere to fetch settings..., believe ConfigurationManager would return the correct value which was set in Azure without consulting to Environment in the past but not any more recently... more research neededCalvin Zhai

2 Answers

0
votes

I see your problem what it is. As we know, the Application Settings priority is higher than the app.config file. Actually it's not about the priority, but the Application Settings will rewrite the config file. So every time you change the Application Settings, the web or webjob will restart.

However if you change the external file like your CommonSettings.config it won't cause restart. Because you could find the config will get nothing changed while your web is running. The value will be retrieved from the shared file.

Here is my conclusion: when you start your web the Application Settings will inject the setting, then these settings will be like just common settings, and the you add the shared setting file, at this time web will retrieve the settings from CommonSettings.config. You could find the priority between app.config and external file here.

If you still have other questions, please let me know.

0
votes

Thanks George for the great points!

It turns out for Webjob, configuration injection happens during deployment time where Azure App Settings get actually written into App.config.

While for normal Web (API) application, it's doing differently where App.config remains untouched but Azure App Settings being injected into ConfigurationManager.AppSettings at run time.

To resolve the issue completely across Web (API) app and WebJob one, I'm building a Visual Studio After-Build script to merge changes from CommonSettings.config into Web.config/App.config and leave the rest as is. So in our app, we simply using ConfigurationManager to resolve settings reliably: Azure setting > Web/App.config > CommonSetting.config