Forem

Mikael Krief
Mikael Krief

Posted on

Using Azure App Configuration for Enhancing .NET Application Configuration

As a .NET developer, you’ve likely worked extensively with the web.config file to manage application settings. Traditionally, keys in the AppSettings section have been the go-to for configuration needs. While this approach works well for simple setups, modern cloud-based applications demand more flexibility, scalability, and centralized configuration management. Azure App Configuration is a great tool to meet these requirements.

In this blog post, I’ll share how you can enhance your application’s configuration system by integrating Azure App Configuration alongside your existing web.config file, using ConfigurationBuilders. This approach ensures minimal code changes while significantly improving your configuration management. Importantly, this configuration will continue to support keys from AppSettings without requiring Azure App Configuration.

The Current Setup: web.config

The web.config file has been a cornerstone of .NET application configuration. Here’s an example of a typical AppSettings section:

<configuration>
  <appSettings>
    <add key="ApiUrl" value="https://api.example.com" />
    <add key="FeatureToggle" value="true" />
  </appSettings>
</configuration>
Enter fullscreen mode Exit fullscreen mode

While this works for basic setups, managing configurations across multiple environments (e.g., development, staging, production) becomes challenging. You often end up duplicating configurations or manually updating them for each environment. This approach is error-prone and lacks centralized control.

Introducing Azure App Configuration

Azure App Configuration provides a centralized and scalable way to manage application settings. It allows you to store configuration values in a single location, making it easier to manage and update settings across environments.

Some key features include:

  • Centralized management of configuration settings.
  • Integration with Azure Key Vault for secure storage.
  • Versioning and labeling of settings for different environments.
  • Feature management capabilities.

The Challenge: Minimizing Code Changes

Migrating entirely to Azure App Configuration can be time-consuming, especially for legacy applications. To address this, we can use ConfigurationBuilders to integrate Azure App Configuration into the existing web.config setup seamlessly while ensuring that the application can still use values defined directly in AppSettings if Azure App Configuration is not available.

Using ConfigurationBuilders

ConfigurationBuilders is a feature introduced in .NET Framework 4.7.1 that allows dynamic configuration values to be injected into your application at runtime. By leveraging this feature, you can combine values from web.config, Azure App Configuration, and other sources without requiring major code changes.

Here’s how you can implement it:

1- Install the Required NuGet Packages

Add the following NuGet packages to your project:

Install-Package Microsoft.Configuration.ConfigurationBuilders.AzureAppConfiguration
Install-Package Microsoft.Configuration.ConfigurationBuilders.Environment
Enter fullscreen mode Exit fullscreen mode

2- Modify the web.config File

Update your web.config file to use ConfigurationBuilders:

<configuration>
  <configBuilders>
    <builders>
      <add name="AzureConfigBuilder" 
           type="Microsoft.Configuration.ConfigurationBuilders.AzureAppConfigurationBuilder, Microsoft.Configuration.ConfigurationBuilders.AzureAppConfiguration" 
           connectionString="${ConnectionString}" 
           mode="greedy" 
           optional="true" />
      <add name="EnvironmentConfigBuilder" 
           type="Microsoft.Configuration.ConfigurationBuilders.EnvironmentConfigBuilder, Microsoft.Configuration.ConfigurationBuilders.Environment" 
           mode="greedy" />
    </builders>
  </configBuilders>

  <appSettings configBuilders="AzureConfigBuilder,EnvironmentConfigBuilder">
    <add key="ConnectionString" value="Endpoint=https://<your-app-config-name>.azconfig.io;Id=<id>;Secret=<secret>" />
    <add key="ApiUrl" value="https://api.example.com" />
    <add key="FeatureToggle" value="true" />
  </appSettings>
</configuration>
Enter fullscreen mode Exit fullscreen mode

In this setup:

  • The configBuilders section specifies both AzureAppConfigurationBuilderand EnvironmentConfigBuilder.
  • The connectionString is referenced using the ${ConnectionString} syntax, allowing it to be dynamically resolved from the AppSettings section.
  • The mode="greedy" attribute ensures that all matching keys from each builder are considered instead of stopping at the first match.
  • The optional="true" attribute ensures that the application continues to work even if Azure App Configuration is unavailable.
  • The EnvironmentConfigBuilder allows values to be overridden by environment variables.
  • Existing keys in AppSettings are augmented or overridden by values from Azure App Configuration or environment variables.

3- Add Settings to Azure App Configuration and Environment Variables

Navigate to your Azure App Configuration instance in the Azure Portal and add the required settings. Additionally, set environment variables for settings that you want to override dynamically.

For example:

Azure App Configuration:

Environment Variables:

4- Run and Test the Application

When your application runs, the ConfigurationBuildersdynamically fetch the settings from Azure App Configuration, environment variables, and the web.config file. The priority is determined by the order in which builders are listed in the configBuilders section.

Configuration Order

The effective configuration values are determined by the following priority, based on the order of ConfigurationBuilders in the web.config:

  1. Azure App Configuration (AzureAppConfigurationBuilder)
  2. Environment Variables (EnvironmentConfigBuilder)
  3. web.config Values

By setting mode="greedy", all sources are checked, ensuring that no configuration values are missed, and all possible sources are used for retrieving settings.

Conclusion

By using ConfigurationBuilders with mode="greedy", you can ensure that all available configuration sources are considered when resolving keys. This approach enhances flexibility and ensures that your application dynamically adapts to available configurations while maintaining backward compatibility. Start enhancing your application today with Azure App Configuration and EnvironmentConfigBuilder for centralized and dynamic configuration management!

Top comments (0)