Skip to content

ConfigurationProvider for App.config / Web.config settings

License

Notifications You must be signed in to change notification settings

gcsizmadia/EgonsoftHU.Extensions.Configuration

Repository files navigation

Egonsoft.HU Configuration Extensions

GitHub

Extensions for Microsoft.Extensions.Configuration.

EgonsoftHU.Extensions.Configuration.ConfigurationManager

Nuget Nuget

App.config/Web.config configuration provider implementation for Microsoft.Extensions.Configuration.

Introduction

When migrating from .NET Framework to .NET Core and your App.config or Web.config file contains lots of settings it might be desirable to be able to incrementally transfer those settings into the new appsettings.json file so that you can incrementally replace the static uses of ConfigurationManager with an injectable IConfiguration instance.

Releases

You can download the package from nuget.org.

You can find the release notes here.

Usage

Suppose you have an App.config file with the following content.

<?xml version="1.0" encoding="utf-8"?>
<configuration>
  <appSettings>
    <!-- legacy setting -->
    <add key="MyAppSetting" value="..." />
    <!--
    Temporarily added to this file during migration.
    The key uses the new format so that 
    later it can be read the same way from appsettings.json file.
    -->
    <add key="SomeApi:ApiKey" value="..." />
  </appSettings>
  <connectionStrings>
    <add name="MyConnectionString" connectionString="..." />
  </connectionStrings>
  <!-- The rest is omitted for clarity. -->
</configuration>

First you need to add the configuration provider.

using Microsoft.Extensions.Configuration;

private static IHostBuilder CreateHostBuilder(string[] args)
{
    return
        Host.CreateDefaultBuilder(args)
            .ConfigureWebHostDefaults(
                webHostBuilder =>
                {
                    webHostBuilder.UseStartup<Startup>();
                }
            )
            .ConfigureAppConfiguration(
                (hostBuilderContext, configurationBuilder) =>
                {
                    // Add the configuration provider here.
                    configurationBuilder.AddConfigurationManager();
                }
            );
}

Then you can read the settings using the injected IConfiguration instance, e.g. in the Startup.cs file.

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

    public IConfiguration Configuration { get; }

    public void ConfigureServices(IServiceCollection services)
    {
        // Get the legacy app setting.
        string myAppSetting = Configuration["MyAppSetting"];

        // Get the new setting
        string apiKey1 = Configuration.GetSection("SomeApi")["ApiKey"];

        // Alternatively you can get the new setting also this way.
        string apiKey2 = Configuration["SomeApi:ApiKey"];

        // Get the connection string.
        string myConnectionString1 = Configuration.GetConnectionString("MyConnectionString");

        // Alternatively you can get the connection string also this way.
        string myConnectionString2 = Configuration["ConnectionStrings:MyConnectionString");

        // The rest is omitted for clarity.
    }
}

Credits

Ben Foster