简体   繁体   中英

Azure App Service Application Settings Ignored, Using web.config Instead

I recently deployed an ASP.Net Web API project to our Azure App Service test slot but started receiving an error when making requests to the API endpoints. Through remote debugging, it became clear that the app was extracting my dev connection strings from the deployed web.config file.

The connection strings are supposed to come from the Application Settings we set up via the Azure Portal - and, in previous deployments, they were - but that's not the case.

Why would this happen and what can be done to ensure the correct behaviour occurs? We absolutely don't want our production database secrets being put into GIT via the web.config ...

I recently experienced the same problem and fixed it:

  • In Azure App Services, the machine-wide web.config file is located at D:\\Windows\\Microsoft.NET\\Framework64\\v4.0.30319\\Config\\web.config .

  • This file differs to a normal machine-wide web.config file because it has this extra element:

     <system.web> ... <compilation> <assemblies> <add assembly="EnvSettings, Version=1.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" /> ... </assemblies> </compilation> </system.web>
  • The EnvSettings.dll assembly is located inside D:\\Program Files\\IIS\\Microsoft Web Hosting Framework (unfortunately this directory is access-controlled and I can't get into it).

    • But EnvSettings.dll is mirrored in the GAC, so I was able to copy it from there.
  • Inside EnvSettings.dll is an [assembly: PreApplicationStartMethod] attribute that runs a method in EnvSettings.dll which copiesthe APPSETTING_ and database connection-string settings from Environment Variables into the .NET Framework ConfigurationManager.AppSettings and ConfigurationManager.ConnectionStrings collections.

    • Note this copying only happens once (during application startup), and only in the first AppDomain - so if you have other AppDomain instances in your application they won't see the updated ConfigurationManager .
  • Therefore, if you see that your Azure Portal configuration settings for your App Service are not being used when you dump your ConfigurationManager , then the following is likely happening:

    • You used <clear /> in your <compilation><assemblies> element, which stops EnvSettings.dll from being loaded at all.
      • In which case you need to either add back the <add assembly="EnvSettings... element from above to your own web.config , or find some other way to load it.
        • I don't recommend saving EnvSettings.dll locally and adding an assembly reference to your project, as EnvSettings.dll is part of the Microsoft Web Hosting Framework.
    • Or you have code that is clearing or resetting ConfigurationManager after EnvSettings populates it for you.
    • Or something else is going on that I have no idea about!

As an alternative to having EnvSettings.dll copy your settings over, another option is to copy the environment-variables over yourself - and as you control the code that does this it means you can call it whenever you need to (eg if you ever reset them).

Here's the code I used:

public static class AzureAppSettingsConfigurationLoader
{
    public static void Apply()
    {
        foreach( DictionaryEntry environmentVariable in Environment.GetEnvironmentVariables() )
        {
            String name  = (String)environmentVariable.Key;
            String value = (String)environmentVariable.Value;
            
            if( name.StartsWith( "APPSETTING_", StringComparison.OrdinalIgnoreCase ) )
            {
                String appSettingName = name.Substring( "APPSETTING_".Length );
                ConfigurationManager.AppSettings[ appSettingName ] = value;
            }
            else if( name.StartsWith( "SQLAZURECONNSTR_", StringComparison.OrdinalIgnoreCase ) )
            {
                String csName = name.Substring( "SQLAZURECONNSTR_".Length );

                ConfigurationManager.ConnectionStrings.Add( new ConnectionStringSettings( csName, value, providerName: ""System.Data.SqlClient" ) );
            }
        }
    }
}

See my sample here: http://mvc5appsettings.azurewebsites.net/

// My web.config also has a "HERO_TEXT" key in
// that reads "Value from web.config"
string hero = ConfigurationManager.AppSettings["HERO_TEXT"];

拉取应用设置

Wiki page on App Settings for .NET:
https://github.com/projectkudu/kudu/wiki/Managing-settings-and-secrets

As already mentioned here, make sure you have that App Setting in the right slot.

As I know, the settings in Azure portal will override existing setting in Web.config. So If you want to ignore the Azure Application settings in portal and use Web.config instead. I am afraid you need to configure the settings in web.config, and remove the same key/pair in Azure portal.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM