• Post author:
  • Reading time:15 mins read

Introduction

Software configuration is a crucial part of any application’s life cycle. It allows developers to mold an application to its environment, making it flexible and adaptable. In ASP.NET Core, the WebApplicationBuilder plays a crucial role in this configuration process. This article aims to shed light on the different ways to configure the WebApplicationBuilder using different configuration providers, and also discuss the common pros and cons of each method.

The Foundation of ASP.NET Core Configuration: The .NET Configuration System

At its core, the ASP.NET Core configuration system derives from the broader .NET configuration system. The .NET configuration system provides a general-purpose framework for dealing with configuration data, offering a way to retrieve values for use in .NET applications. However, ASP.NET Core extends this base system to provide additional capabilities tailored for web application environments. This article will discuss how ASP.NET Core’s configuration system builds upon its .NET counterpart and highlight the differences between the two.

In the .NET configuration system, developers primarily deal with ConfigurationBuilder and ConfigurationRoot classes. Configuration data is accessed as a simple key-value pair, and the sources of this data can include JSON files, XML files, INI files, and even command-line arguments. This configuration system is flexible and extensible, providing a solid foundation for ASP.NET Core’s configuration system.

ASP.NET Core takes the .NET configuration system and enhances it by introducing a variety of web-specific configuration providers. These providers include capabilities to pull configuration data from environment variables, a system designed to prevent the accidental check-in of sensitive data during the development process, and from other providers.

Furthermore, ASP.NET Core introduces the IConfiguration and IConfigurationRoot interfaces, designed to abstract away the complexity of dealing with different configuration sources. It uses dependency injection to provide access to the configuration data throughout the application, making it easier for developers to work with configuration values.

One of the most significant differences between the two systems is the introduction of the WebApplicationBuilder in ASP.NET Core, which abstracts the process of creating a ConfigurationBuilder, adding common configuration sources, and building the IConfiguration. This makes setting up configuration in ASP.NET Core applications simpler and more streamlined.

In conclusion, while ASP.NET Core’s configuration system is built upon the .NET configuration system, it enhances and tailors the system to meet the unique needs of web application environments. With added features, extended providers, and streamlined APIs, ASP.NET Core’s configuration system provides a powerful and flexible way to manage application configuration.

A Closer Look at Configuration in ASP.NET Core

ASP.NET Core comes with a robust configuration system. The WebApplicationBuilder provides a rich API to set up your application. It allows you to take advantage of configuration sources like JSON files, environment variables, command-line arguments, or even custom configuration providers.

Configuring via ‘appsettings.json’

The ‘appsettings.json’ file is one of the most common and preferred ways to manage configurations in ASP.NET Core applications. It provides an easily readable and editable JSON structure where you can define settings that will be loaded into the application on startup.

To configure your application using ‘appsettings.json’, you need to call the ConfigureAppConfiguration method on the WebApplicationBuilder instance. This method allows you to define a delegate to configure the provided ConfigurationBuilder.

var builder = WebApplication.CreateBuilder(args);
builder.ConfigureAppConfiguration((hostingContext, config) =>
{
    config.AddJsonFile("appsettings.json", optional: true, reloadOnChange: true);
});

Pros:

  1. Easy to use: The ‘appsettings.json’ file is easy to read and write, making it an excellent choice for managing configuration data.
  2. Hot Reload: The ‘appsettings.json’ supports hot reload. This means the application can detect changes in the file and apply these changes without requiring a restart.

Cons:

  1. Sensitive Data Exposure: Storing sensitive data, like connection strings or API keys, can lead to security vulnerabilities if the file is not appropriately secured.
  2. Environment Dependency: The configuration is tightly coupled with the application. Therefore, managing different environments (like development, staging, and production) could become cumbersome.

Configuring via Environment Variables

Environment variables are a common way to set configuration values on the host machine. These are particularly useful for injecting configuration values without changing the application’s code or relying on external files.

To configure your application using environment variables, you can again use the ConfigureAppConfiguration method:

var builder = WebApplication.CreateBuilder(args);
builder.ConfigureAppConfiguration((hostingContext, config) =>
{
    config.AddEnvironmentVariables();
});

Pros:

  1. Platform Independent: Environment variables can be used across different platforms and languages, providing a universal method for configuration.
  2. Secure: It’s a safer way to store sensitive data as these can be set up in the server environment and remain inaccessible to unauthorized users.

Cons:

  1. Less Intuitive: As environment variables are not stored in a structured file like ‘appsettings.json’, it could be more challenging to track all available configuration values.
  2. Potential for Conflicts: Environment variables are system-wide, so they could potentially conflict with other applications running on the same system.

Other built-in Configuration Providers in ASP.NET Core

ASP.NET Core provides a range of built-in configuration providers that allow you to consume configuration data from various sources. This flexibility ensures that you can tailor your application’s configuration to fit the requirements of any environment or use case. Below are the built-in configuration providers that come with ASP.NET Core:

1. In-Memory .NET objects

The MemoryConfigurationProvider is used for in-memory collections. This provider is primarily used for testing or when you need to override values provided by other providers:

builder.ConfigureAppConfiguration((hostingContext, config) =>
{
    var dict = new Dictionary<string, string>
    {
        {"MemoryCollectionKey", "Value from memory collection"}
    };
    config.AddInMemoryCollection(dict);
});

2. XML Files

The XmlConfigurationProvider is used for XML files. While not as commonly used as JSON, it’s there if you need it:

builder.ConfigureAppConfiguration((hostingContext, config) =>
{
    config.AddXmlFile("customSettings.xml", optional: true, reloadOnChange: true);
});

3. Command-Line Arguments

The CommandLineConfigurationProvider is used for command-line arguments. This can be handy when you need to override some configuration values during startup:

builder.ConfigureAppConfiguration((hostingContext, config) =>
{
    config.AddCommandLine(args);
});

4. Azure Key Vault

The AzureKeyVaultConfigurationProvider is used to retrieve configuration data stored in Azure Key Vault. It provides a secure and centralized way to manage application secrets.

builder.ConfigureAppConfiguration((hostingContext, config) =>
{
    config.AddAzureKeyVault(
        "<Your Vault Uri>",
        "<Your Client Id>",
        "<Your Client Secret>"
    );
});

5. User Secrets

The UserSecretsConfigurationProvider is used for storing sensitive data during the development process. The User Secrets store data outside the project tree, thus preventing accidental check-in into source control:

builder.ConfigureAppConfiguration((hostingContext, config) =>
{
    if (hostingContext.HostingEnvironment.IsDevelopment())
    {
        config.AddUserSecrets<Program>();
    }
});

Understanding Configuration Provider Priority in ASP.NET Core

ASP.NET Core allows you to use multiple configuration providers in your applications, and each of them can potentially modify the same configuration keys. So, how does the system decide which value to use? The answer lies in the order of precedence among the configuration providers.

When you add multiple configuration providers, the last one has the highest priority. This is because each successive provider overlays its keys on top of the previous ones. This means if you define the same configuration key in two different providers, the value from the provider added last will take precedence.

Let’s take an example of an application configured to use ‘appsettings.json’ and environment variables:

var builder = WebApplication.CreateBuilder(args);
builder.ConfigureAppConfiguration((hostingContext, config) =>
{
    config.AddJsonFile("appsettings.json", optional: true, reloadOnChange: true);
    config.AddEnvironmentVariables();
});

In this scenario, if you have a key defined in both ‘appsettings.json’ and as an environment variable, the environment variable will take precedence because the environment variable configuration provider was added after the JSON file configuration provider.

The order in which configuration providers are added is crucial. If you reverse the order of the providers in the above example, the ‘appsettings.json’ file will override any keys defined in the environment variables.

Keep this priority rule in mind when defining your configuration providers, as it can significantly impact how your application behaves in different environments. This approach offers a high degree of flexibility, enabling you to fine-tune your configuration strategy to suit your specific needs.

Accessing Configuration settings in ASP.NET Core

Understanding how to configure your application is just the first step. The real power comes from leveraging these configurations within your application. In this paragraph, we’ll see how to access configuration settings in ASP.NET Core, both directly and using the ASP.NET Dependency Injection (DI) container.

Accessing Configuration Directly in the Startup Class

The Startup class in an ASP.NET Core application is where you configure your app’s request pipeline and services. You can access the configuration system directly in the Startup class. Here’s how you do it:

public class Startup
{
    public IConfiguration Configuration { get; }

    public Startup(IConfiguration configuration)
    {
        Configuration = configuration;
    }

    public void ConfigureServices(IServiceCollection services)
    {
        var mySetting = Configuration["MySetting"];
        // ...
    }

    // ...
}

In the above code, the IConfiguration instance is injected into the Startup constructor. You can then use this instance to access configuration settings using the indexer.

Accessing Configuration via Dependency Injection

One of the powerful features of ASP.NET Core is its built-in support for dependency injection. You can inject the IConfiguration interface into your controllers or any class that supports dependency injection. Here’s an example of how to do this:

public class MyController : Controller
{
    private readonly IConfiguration _configuration;

    public MyController(IConfiguration configuration)
    {
        _configuration = configuration;
    }

    public IActionResult Index()
    {
        var mySetting = _configuration["MySetting"];
        // ...
    }
}

In the above code, the IConfiguration instance is injected into the controller’s constructor. You can then use this instance to access your configuration settings.

Configuration Binding to POCO Classes

ASP.NET Core also supports configuration binding, which is the process of mapping configuration data to Plain Old CLR Objects (POCOs). This can make your code cleaner and easier to work with. Here’s an example:

public class MySettings
{
    public string Key1 { get; set; }
    public string Key2 { get; set; }
}

public class Startup
{
    public IConfiguration Configuration { get; }

    public Startup(IConfiguration configuration)
    {
        Configuration = configuration;
    }

    public void ConfigureServices(IServiceCollection services)
    {
        services.Configure<MySettings>(Configuration.GetSection("MySettings"));
        // ...
    }

    // ...
}

In the above code, we’ve defined the class MySettings that matches the structure of our configuration data. We then use the Configure method to bind the MySettings section of the configuration to instances of MySettings.

You can then access these settings in your controllers by injecting IOptions<MySettings>:

public class MyController : Controller
{
    private readonly MySettings _mySettings;

    public MyController(IOptions<MySettings> mySettings)
    {
        _mySettings = mySettings.Value;
    }

    public IActionResult Index()
    {
        var key1 = _mySettings.Key1;
        // ...
    }
}

Conclusion

ASP.NET Core offers a flexible and powerful configuration system, allowing developers to choose the best approach for their application’s unique needs. All the built-in configuration providers offer some advantages, but they also come with their caveats. Therefore, a well-thought-out combination of these approaches often yields the best results, providing both flexibility and security. Moreover, by understanding how to access settings within your application, you can create applications that are easier to manage and more adaptable to change. Remember, picking the right configuration management is a crucial step in building scalable, maintainable, and secure ASP.NET Core applications.