Calvin Zhai
Calvin Zhai

Reputation: 1

Cannot Resolve Configuration in Azure Application Settings When WebJob Starts

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!

Upvotes: 0

Views: 1471

Answers (2)

Calvin Zhai
Calvin Zhai

Reputation: 1

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

Upvotes: 0

George Chen
George Chen

Reputation: 14334

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.

Upvotes: 0

Related Questions