/AnyConfig

A configuration provider for .Net Framework / .Net Core without reliance on Microsoft's configuration dependencies

Primary LanguageC#MIT LicenseMIT

AnyConfig

nuget nuget Build status Codacy Badge Codacy Badge

A .net configuration library to make configuration of multi-target applications easier.

Description

AnyConfig makes configuration on solutions which mix .Net Core and .Net Framework projects easier. It abstracts away ConfigurationManager and IConfiguration loading with no dependencies on Microsoft implementation. You can instead use ConfigurationManager to load either json or xml configuration files, as well as the IConfiguration interface. This allows you to upgrade to json configuration files even for older projects, or have multi-target projects which use different configuration formats without extra code!

Installation

PM> Install-Package Any-Config

Features

  • Backwards compatible interface using ConfigurationManager for Xml and Json
  • Supports IConfiguration interface for Xml and Json
  • Supports generics for simple configuration value lookups
  • Automatic discovery of configuration files for .Net Core or .Net Framework projects
  • Legacy Xml encrypted sections (DPAPI and RSA) are supported
  • [todo] Yaml support coming soon

Usage

Simplest usage is using the dedicated generics interface:

var isEnabled = Config.Get<bool>("IsEnabled");

You can specify a default value for settings that aren't required to exist:

var intValue = Config.Get<int>("Port", 443);

You can also bind your own configuration class:

var testConfiguration = Config.Get<MyTestConfiguration>();

Grab an IConfiguration for .net core without any Microsoft extensions:

var config = Config.GetConfiguration();
var testConfiguration = config.Get<MyTestConfiguration>();

If you need, use the legacy ConfigurationManager:

var isEnabled = ConfigurationManager.AppSettings["IsEnabled"];

The built-in ConfigurationManager supports generics:

var isEnabled = ConfigurationManager.AppSettings["IsEnabled"].As<bool>();

It also supports reading from json:

ConfigurationManager.ConfigurationFilename = "appsettings.json";
var isEnabled = ConfigurationManager.AppSettings["IsEnabled"].As<bool>();
// appsettings.json
{
  "IsEnabled": true
}

Advanced Usage

See the wiki