Skip to content

A small try of compensation of non-existing Microsoft.Extensions.Configuration.AppConfig

License

Notifications You must be signed in to change notification settings

rychlym/Configuration.AppConfig

Repository files navigation

Configuration.AppConfig

NuGet NuGet

Overview

The .NET Core configuration supports variety of configuration sources, like JSON file, XML file, Ini file, environment variables, command line.

The "glory" of the legacy app.config has definitelly gone away. However, if there is still an unknown reason for using this older source, this small library culd serve. This simple provider can read only the appSettings or connectionStrings sections. The current last version has not changed functionality, just supports the currently supported framewors as .NET6, .NET7 and .NET8, and keeps the targeting the .NET Standard 2.0.

In the case of more complex app.config, I would definitelly encourage you to migrate it to the appsettings.json. (Erlier days, It also was on the plate to consider the general XML file provider (referenced as the Microsoft.Extensions.Configuration.Xml NuGet package.)

Installation

Install the [NuGet package][nuget] into your project.

PM> Install-Package Configuration.AppConfig
$ dotnet add package Configuration.AppConfig

Basics of Usage

  1. Register the provider in the ConfigurationBuilder a similar way as for the other provider types would be.
	var builder = new ConfigurationBuilder();			
	builder.AddAppConfig();								
  1. Build the configuration (an object implementing the IConfiguration interface)
	var config = builder.Build();
  1. Use the configuration the standard way to read the appSettings or the connectionStrings (Please reference the Microsoft.Extensions.Configuration.Binder NuGet package in order to use the GetValue<T>() method)
	var tmpDirectory = config["appSettings:tmpDir"];
	var dbConnStr = config["connectionStrings:DbConn"];
	
	var threshold = config.GetValue<double>("appSettings:threshold");

Provider Parameters

It can be specified, how the appSettings and connectionString sections will be named when getting the value. Then e.g. used together with the JSON file configuration source with the same naming. (Please reference the Microsoft.Extensions.Configuration.Json NuGet package in order to use the AddJsonFile() method).

	var builder = new ConfigurationBuilder()			
		.AddAppConfig("AppSettings", "ConnectionStrings")
		.AddJsonFile("appsettings.json", optional: true);
	// reads either from the legacy app.config (appSettings, connectionStrings sections) or from JSON with AppSettings, ConnectionStrings sections
	var tmpDirectory = config["appSettings:tmpDir"];
	var dbConnStr = config["connectionStrings:DbConn"];
	...

Useful Links

About

A small try of compensation of non-existing Microsoft.Extensions.Configuration.AppConfig

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages