SebaNowy
SebaNowy

Reputation: 11

.Net8 and System.Configuration.ConfigurationManager.AppSettings is ignoring Azure AppService configuration

I am in the process of migrating an application written in .NET Framework 4.8 to .NET 8. The applications are hosted on Azure App Services. We heavily utilize Azure DevOps for the deployment process. Our applications use System.ConfigurationManager.AppSettings to read configuration values. In .NET 8, there is IConfiguration object available. I really don't want to migrate to this way of reading configuration because it would involve significant code changes and affect many of our NuGet packages. In .NET 8, there is a NuGet package available called System.Configuration.ConfigurationManager.

The problem is that System.Configuration.ConfigurationManager.AppSettings does not see values from Azure AppService configuration, only from the AppSettings in the app.config file.

In .NET Framework, if the key in the AppSettings section of the app.config file was the same as in the Azure App Service configuration, the value would be retrieved from Azure. Why doesn't this work now?

enter image description here

enter image description here

enter image description here

I've tried System.Configuration.ConfigurationManager package but it's not working like expected.

Upvotes: 1

Views: 1939

Answers (1)

Jason Pan
Jason Pan

Reputation: 21949

According to my own understanding, the main significance of the existence of System.Configuration.ConfigurationManager in .net core should be compatible with the .net framework, which can be more convenient when migrating.

For Azure App Service, the .NET framework uses System.Configuration.ConfigurationManager, and .NET core uses IConfiguration, which is designed to read configurations based on a standards-based framework.

To realize that it is also possible to use System.Configuration.ConfigurationManager to read the configuration, we need to create a custom ConfigurationBridge to bridge it.

ConfigurationBridge.cs

using Newtonsoft.Json.Linq;

namespace TestAppService
{
    public class ConfigurationBridge
    {
        public static void LoadConfiguration()
        {
            LoadAppSettingsFromJson("appsettings.json");
            LoadAzureAppServiceSettings();
        }

        private static void LoadAppSettingsFromJson(string filePath)
        {
            if (!File.Exists(filePath)) return;

            var json = File.ReadAllText(filePath);
            var jsonObject = JObject.Parse(json);

            foreach (var prop in jsonObject.Properties()) 
            {
                var key = prop.Name;
                var value = prop.Value.ToString();
                System.Configuration.ConfigurationManager.AppSettings.Set(key, value);
            }
        }


        private static void LoadAzureAppServiceSettings()
        {
            foreach (var key in Environment.GetEnvironmentVariables().Keys)
            {
                var value = Environment.GetEnvironmentVariable(key.ToString());
                System.Configuration.ConfigurationManager.AppSettings.Set(key.ToString(), value);
            }
        }
    }
}

Register it in Program.cs

enter image description here

Use it in controller

public IActionResult Index()
{
    ViewBag.TestKey = System.Configuration.ConfigurationManager.AppSettings["testkey"];
    //_configuration["TestKey"];
    return View();
}

Test Result

Disable ConfigurationBridge

enter image description here

Enable ConfigurationBridge

enter image description here

Upvotes: 1

Related Questions