Config.Net 5.0.2

There is a newer version of this package available.
See the version list below for details.
dotnet add package Config.Net --version 5.0.2
NuGet\Install-Package Config.Net -Version 5.0.2
This command is intended to be used within the Package Manager Console in Visual Studio, as it uses the NuGet module's version of Install-Package.
<PackageReference Include="Config.Net" Version="5.0.2" />
For projects that support PackageReference, copy this XML node into the project file to reference the package.
paket add Config.Net --version 5.0.2
#r "nuget: Config.Net, 5.0.2"
#r directive can be used in F# Interactive and Polyglot Notebooks. Copy this into the interactive tool or source code of the script to reference the package.
// Install Config.Net as a Cake Addin
#addin nuget:?package=Config.Net&version=5.0.2

// Install Config.Net as a Cake Tool
#tool nuget:?package=Config.Net&version=5.0.2

Config.Net NuGet open collective backers and sponsors Nuget

A comprehensive, easy to use and powerful .NET configuration library, fully covered with unit tests and tested in the wild on thousands of servers and applications.

This library eliminates the problem of having configuration in different places, having to convert types between different providers, hardcoding configuration keys across the solution, depending on specific configuration source implementation. It's doing that by exposing an abstract configuration interface and providing most common implementation for configuration sources like app.config, environment variables etc.

Abstract

Index

Quick Start

Usually developers will hardcode reading cofiguration values from different sources like app.config, local json file etc. For instance, consider this code example:

var clientId = ConfigurationManager.AppSettings["AuthClientId"];
var clientSecret = ConfigurationManager.AppSettings["AuthClientSecret"];

You would guess that this code is trying to read a configuration setting from the local app.config file by name and that might be true, however there are numerous problems with this approach:

  • settings are referenced by a hardcoded string name which is prone to typos and therefore crashes in runtime.
  • there is no easy way to find out where a particular setting is used in code, except for performing a fulltext search (provided that the string was not mistyped)
  • if you decide to store configuration in a different place the code must be rewritten.

Welcome to Config.Net which solves most of those problems. Let's rewrite this abomination using Config.Net approach. First, we need to define a configuration container which describes which settings are used in your application or a library:

Declare settings interface

using Config.Net;

public interface IMySettings
{
    string AuthClientId { get; }

    string AuthClientSecret { get; }
}

These interface members describe the values you are using in code and look exactly like anything else in the code. You can pass this interface around inside your application like nothing happened.

In order to instantiate this interface and bind it to application settings use ConfigurationBuilder<T> class:

IMySettings settings = new ConfigurationBuilder<IMySettings>()
   .UseAppConfig()
   .Build();

This is literally all you have to do. Configuration builder is an entry to creating instances of your interface and underneath it creates a proxy class which intercepts calls to properties and fetches values from underlying configured stores.

Which Data Types are Supported?

Not all of the types can be used in the properties, because Config.Net needs to know how to convert them to and from the underlying stores. Out of the box basic .NET types (bool, double, int, long, string, TimeSpan, DateTime, Uri, Guid) are supported. Two more types are worth special mentioning:

System.Net.NetworkCredential

Is a handy built-in .NET class for holding information with username, password and domain. In reality those three fields are almost always enough to hold connection information to remote servers. The following format is understood: username:password@domain and all parts are optional.

String Arrays

Encoded using a command-line syntax:

  • values are separated by a space i.e. value1 value2
  • if you need spaces inside values you must take it in quotes i.e. "value with space" valuewithoutspace
  • quotes inside values must be escaped using a double quote ("") and the value itself should be quoted i.e. "value with ""quotes""""

It's easy to add a new type by implementing ITypeParser interface.

Using Multiple Sources

ConfigurationBuilder<T> is used to instantiate your configuration interface. You can use it to add multiple configuration sources. To get the list of sources use IntelliSense (type dot-Use):

Intellisense00

The order in which sources are added is important - Config.Net will try to read the source in the configured order and return the value from the first store where it exists.

Changing property behaviour

Option attribute can be used to annotate interface properties with extra behaviour.

Aliases

In case your property is named different to C# property name you can alias it:

public interface IMySettings
{
   [Option(Alias = "clientId")]
   string AuthClientId { get; }
}

which makes Config.Net to look for "clientId" when reading or writing.

Default values

When a property doesn't exist in any of the stores or you just haven't configured any stores at all, you will receive a default value for the property type (0 for int, null for string etc.). However, it's sometimes useful to have a different value returned as a default instead of handling that in you code. In order to do that you can use the DefaultValue property on the attribute:

public interface IMySettings
{
   [Option(Alias = "clientId", DefaultValue = "n/a")]
   string AuthClientId { get; }
}

Now when reading the value will be read as n/a instead of just null. DefaultValue property is of type object therefore the type of the value you assign to it must match the property type. If this is not the case, you will receive InvalidCastException explaining where the problem is during the .Build() stage.

However, you can set the property value to string no matter what the type is, as long as it's parseable to that type in runtime using any of the parsers.

DefaultValueAttribute

Config.Net also supports DefaultValueAttribute as an alternative to specifying default values. This allows your interfaces not to have any dependency on Config.Net library. Following definitions have the same effect:

public interface IMySettings
{
   [Option(DefaultValue = "n/a")]
   string AuthClientId { get; }
}
public interface IMySettings
{
   [DefaultValue("n/a")]
   string AuthClientId { get; }
}

Writing Settings

Some configuration stores support writing values. This can be checked by interrogating IConfigStore.CanWrite property. You can write the value back by simply setting it's value:

c.AuthClientId = "new value";

Config.Net will write the value to the first store which supports writing. If none of the stores support writing the call will be ignored.

Of course in order for a property to be writeable you need to declare it as such in the interface:

string AuthClientId { get; set; }

Sponsorship

This framework is free and can be used for free, open source and commercial applications. Config.Net (all code, NuGets and binaries) are under the MIT License (MIT). It's battle-tested and used by many awesome people and organisations. So hit the magic ⭐️ button, we appreciate it!!! 🙏 Thx!

The core team members, Config.Net contributors and contributors in the ecosystem do this open source work in their free time. If you use Config.Net, and you'd like us to invest more time on it, please donate. This project increases your income/productivity/usabilty too.

Why charge/sponsor for open source?

Backers

Become a backer and show your support to our open source project.

alternate text is missing from this package README image

Sponsors

Does your company use Config.Net? Ask your manager or marketing team if your company would be interested in supporting our project. Support will allow the maintainers to dedicate more time for maintenance and new features for everyone. Also, your company's logo will show here - who doesn't want a little extra exposure?

alternate text is missing from this package README image

Special Thanks

Thanks to JetBrains for kindly providing an open-source license to their amazing Rider IDE for Open Source Development. Rider logo

Product Compatible and additional computed target framework versions.
.NET net5.0 is compatible.  net5.0-windows was computed.  net6.0 is compatible.  net6.0-android was computed.  net6.0-ios was computed.  net6.0-maccatalyst was computed.  net6.0-macos was computed.  net6.0-tvos was computed.  net6.0-windows was computed.  net7.0 was computed.  net7.0-android was computed.  net7.0-ios was computed.  net7.0-maccatalyst was computed.  net7.0-macos was computed.  net7.0-tvos was computed.  net7.0-windows was computed.  net8.0 was computed.  net8.0-android was computed.  net8.0-browser was computed.  net8.0-ios was computed.  net8.0-maccatalyst was computed.  net8.0-macos was computed.  net8.0-tvos was computed.  net8.0-windows was computed. 
.NET Core netcoreapp2.0 was computed.  netcoreapp2.1 was computed.  netcoreapp2.2 was computed.  netcoreapp3.0 was computed.  netcoreapp3.1 is compatible. 
.NET Standard netstandard2.0 is compatible.  netstandard2.1 is compatible. 
.NET Framework net461 was computed.  net462 was computed.  net463 was computed.  net47 was computed.  net471 was computed.  net472 was computed.  net48 was computed.  net481 was computed. 
MonoAndroid monoandroid was computed. 
MonoMac monomac was computed. 
MonoTouch monotouch was computed. 
Tizen tizen40 was computed.  tizen60 was computed. 
Xamarin.iOS xamarinios was computed. 
Xamarin.Mac xamarinmac was computed. 
Xamarin.TVOS xamarintvos was computed. 
Xamarin.WatchOS xamarinwatchos was computed. 
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.

NuGet packages (8)

Showing the top 5 NuGet packages that depend on Config.Net:

Package Downloads
BasinFramework

A .NET browser automation framework based on Selenium WebDriver

Config.Net.Yaml

Adds YAML support to Config.Net

dotnet-markdown

.NET CLI Tool to produce markdown documentation from xml documentation

Akarin.Core

Package Description

TakasakiStudio.Lina.Utils

A library containing useful functions.

GitHub repositories (7)

Showing the top 5 popular GitHub repositories that depend on Config.Net:

Repository Stars
hanmin0822/MisakaTranslator
御坂翻译器—Galgame/文字游戏/漫画多语种实时机翻工具
timschneeb/GalaxyBudsClient
Unofficial Galaxy Buds Manager for Windows, macOS, and Linux
Otiel/BandcampDownloader
A Windows app used to download albums from Bandcamp.
Nihlus/Launchpad
An open-source game launcher for your games
automuteus/amonguscapture
Capture of the local Among Us executable state
Version Downloads Last updated
5.2.0 2,770 4/2/2024
5.1.5 63,369 11/21/2022
5.1.4 7,853 11/4/2022
5.1.3 4,096 9/20/2022
5.1.2 3,318 8/3/2022
5.1.1 1,140 8/2/2022
5.1.0 1,292 7/28/2022
5.0.3 3,438 7/26/2022
5.0.2 1,162 7/26/2022
5.0.1 1,560 7/20/2022
5.0.0 861 7/20/2022
4.19.0 77,239 12/9/2021
4.18.0 827 12/9/2021
4.17.0 4,722 9/16/2021
4.16.2 2,016 9/16/2021
4.15.0 51,144 7/29/2020
4.14.25 1,481 7/29/2020
4.14.24 2,234 7/12/2020
4.14.23 44,132 1/16/2020
4.14.20 3,602 1/3/2020
4.14.19 1,446 1/3/2020
4.14.16 5,656 12/10/2019
4.14.15 1,633 12/9/2019
4.14.14 6,253 11/29/2019
4.13.7 40,195 4/23/2019
4.13.5 1,794 4/23/2019
4.13.2 72,432 1/27/2019
4.12.0 1,666 1/23/2019
4.11.0 18,275 10/16/2018
4.10.1 5,531 8/6/2018
4.10.0 14,457 5/29/2018
4.9.1 2,797 4/26/2018
4.9.0 1,778 4/26/2018
4.8.0 1,978 4/18/2018
4.7.3 2,644 4/12/2018
4.7.2 2,218 4/8/2018
4.7.0.92 1,854 3/28/2018
4.7.0.89 2,927 1/31/2018
4.6.1.82 4,120 1/4/2018
4.6.0.80 1,861 1/4/2018
4.5.0.76 1,857 12/20/2017
4.4.2.75 1,694 12/18/2017
4.4.1.71 2,788 12/15/2017
4.4.0.68 1,800 12/15/2017
4.4.0.66 1,960 12/13/2017
4.3.1 1,939 12/12/2017
4.2.0 2,097 12/6/2017
4.1.0 2,319 10/26/2017
4.1.0-preview-50 1,518 10/18/2017
4.0.48 1,893 10/17/2017
4.0.46 1,796 10/12/2017
4.0.45 1,851 10/12/2017
4.0.44 1,858 10/12/2017
4.0.0-alpha-43 1,533 10/12/2017
4.0.0-alpha-42 1,535 10/12/2017
4.0.0-alpha-41 1,546 10/11/2017
4.0.0-alpha-39 1,572 9/29/2017
4.0.0-alpha-36 1,571 9/28/2017
4.0.0-alpha-35 1,509 9/28/2017
3.3.37 2,403 9/29/2017
3.3.34 1,706 9/28/2017
3.3.32 1,735 9/28/2017
3.3.31 1,708 9/28/2017
3.3.29 1,860 9/25/2017
3.3.28 1,682 9/25/2017
3.3.26 1,723 9/21/2017
3.3.24 4,440 9/6/2017
3.3.17 2,019 8/8/2017
3.3.16 1,717 8/8/2017
3.2.14 1,708 7/31/2017
3.2.13 1,662 7/31/2017
3.2.12 1,560 7/31/2017
3.2.11 1,543 7/31/2017
3.2.9 1,575 7/31/2017
3.2.6 1,653 7/14/2017
3.2.5 1,703 7/10/2017
3.2.4 1,603 7/10/2017
3.2.3 1,628 7/5/2017
3.2.0 2,373 3/28/2017
3.1.2 1,685 3/21/2017
3.1.1 1,628 3/21/2017
3.1.0 1,617 3/21/2017
3.0.1702.1002 1,665 2/10/2017
3.0.1702.1001 1,706 2/10/2017
3.0.1701.3101 1,718 1/31/2017
3.0.1701.1901 1,676 1/19/2017
3.0.1701.1801 1,637 1/18/2017
3.0.1701.1701 1,669 1/17/2017
3.0.1701.1604 1,624 1/16/2017
3.0.1 2,941 10/26/2016
3.0.0 1,598 10/26/2016
2.0.282 1,642 10/7/2016
2.0.281 1,509 10/5/2016
2.0.279 1,487 10/4/2016
2.0.278 1,530 10/4/2016
2.0.277 1,592 10/4/2016
2.0.258-alpha 1,324 8/30/2016
2.0.257-alpha 1,356 8/30/2016
2.0.256-alpha 1,355 8/30/2016
2.0.254-alpha 1,374 8/30/2016
2.0.253-alpha 1,385 8/30/2016
1.3.0.271 1,546 9/22/2016
1.3.0.270 1,545 9/22/2016
1.3.0.269 4,801 9/22/2016
1.3.0.244 2,653 8/23/2016
1.3.0.242 1,580 8/22/2016
1.2.0.241 1,918 7/28/2016
1.2.0.125 3,742 6/14/2016
1.2.0.120 1,808 6/9/2016
1.2.0.115 1,620 6/7/2016
1.2.0.114 1,619 6/7/2016
1.2.0.113 1,588 6/7/2016
1.2.0.100 1,601 6/3/2016
1.2.0 2,476 1/18/2016
1.1.2 1,751 1/4/2016
1.1.1 1,635 1/4/2016
1.1.0 1,631 12/18/2015
1.0.4 1,765 11/20/2015
1.0.3 1,694 11/18/2015
1.0.2 1,702 9/16/2015
1.0.1 1,677 9/12/2015
1.0.0 1,967 9/12/2015