Config.Net
5.0.3
See the version list below for details.
dotnet add package Config.Net --version 5.0.3
NuGet\Install-Package Config.Net -Version 5.0.3
<PackageReference Include="Config.Net" Version="5.0.3" />
paket add Config.Net --version 5.0.3
#r "nuget: Config.Net, 5.0.3"
// Install Config.Net as a Cake Addin #addin nuget:?package=Config.Net&version=5.0.3 // Install Config.Net as a Cake Tool #tool nuget:?package=Config.Net&version=5.0.3
Config.Net
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.
Index
- Configuration Sources
- Nested Interfaces
- Collections
- Binding to Interface Methods
- INotifyPropertyChanged
Quick Start
Usually developers will hardcode reading configuration 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):
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?
- Open-Source Maintainers are Jerks! | Nick Randolph & Geoffrey Huntley
- FOSS is free as in toilet | Geoffroy Couprie
- How to Charge for your Open Source | Mike Perham
- Sustain OSS: The Report
- Open Source Maintainers Owe You Nothing | Mike McQuaid
- Who should fund open source projects? | Jane Elizabeth
- Apply at OSS Inc today| Ryan Chenkie
- The Ethics of Unpaid Labor and the OSS Community | Ashe Dryden
Backers
Become a backer and show your support to our open source project.
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?
Special Thanks
Thanks to JetBrains for kindly providing an open-source license to their amazing Rider IDE for Open Source Development.
Product | Versions 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. |
-
.NETCoreApp 3.1
- Castle.Core (>= 5.0.0)
- System.Configuration.ConfigurationManager (>= 6.0.0)
-
.NETStandard 2.0
- Castle.Core (>= 5.0.0)
- System.Configuration.ConfigurationManager (>= 6.0.0)
-
.NETStandard 2.1
- Castle.Core (>= 5.0.0)
- System.Configuration.ConfigurationManager (>= 6.0.0)
-
net5.0
- Castle.Core (>= 5.0.0)
- System.Configuration.ConfigurationManager (>= 6.0.0)
-
net6.0
- Castle.Core (>= 5.0.0)
- System.Configuration.ConfigurationManager (>= 6.0.0)
NuGet packages (7)
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 |
|
TakasakiStudio.Lina.Utils
A library containing useful functions. |
|
Akarin.Core
Package Description |
|
PSNSharp
Playstation Network API based on the work of Tustin but ported to .Net Standard and with Puppeteer instead of WebBrowser Control (WinForms) Original Copyright is with Tustin: https://github.com/Tustin/PlayStationSharp |
GitHub repositories (9)
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, Linux, and Android
|
|
Otiel/BandcampDownloader
A Windows app used to download albums from Bandcamp.
|
|
aloneguid/parquet-dotnet
Fully managed Apache Parquet implementation
|
|
Nihlus/Launchpad
An open-source game launcher for your games
|
Version | Downloads | Last updated |
---|---|---|
5.3.0-pre.1 | 50 | 11/6/2024 |
5.2.1 | 667 | 11/6/2024 |
5.2.1-pre.1 | 34 | 11/6/2024 |
5.2.0 | 35,180 | 4/2/2024 |
5.1.5 | 87,123 | 11/21/2022 |
5.1.4 | 9,618 | 11/4/2022 |
5.1.3 | 4,542 | 9/20/2022 |
5.1.2 | 3,538 | 8/3/2022 |
5.1.1 | 1,188 | 8/2/2022 |
5.1.0 | 1,343 | 7/28/2022 |
5.0.3 | 3,970 | 7/26/2022 |
5.0.2 | 1,202 | 7/26/2022 |
5.0.1 | 1,633 | 7/20/2022 |
5.0.0 | 922 | 7/20/2022 |
4.19.0 | 91,000 | 12/9/2021 |
4.18.0 | 878 | 12/9/2021 |
4.17.0 | 5,911 | 9/16/2021 |
4.16.2 | 2,050 | 9/16/2021 |
4.15.0 | 55,517 | 7/29/2020 |
4.14.25 | 1,527 | 7/29/2020 |
4.14.24 | 2,275 | 7/12/2020 |
4.14.23 | 45,694 | 1/16/2020 |
4.14.20 | 3,761 | 1/3/2020 |
4.14.19 | 1,481 | 1/3/2020 |
4.14.16 | 5,800 | 12/10/2019 |
4.14.15 | 1,694 | 12/9/2019 |
4.14.14 | 6,364 | 11/29/2019 |
4.13.7 | 41,767 | 4/23/2019 |
4.13.5 | 1,853 | 4/23/2019 |
4.13.2 | 75,356 | 1/27/2019 |
4.12.0 | 1,711 | 1/23/2019 |
4.11.0 | 18,403 | 10/16/2018 |
4.10.1 | 5,605 | 8/6/2018 |
4.10.0 | 14,564 | 5/29/2018 |
4.9.1 | 2,855 | 4/26/2018 |
4.9.0 | 1,812 | 4/26/2018 |
4.8.0 | 2,029 | 4/18/2018 |
4.7.3 | 2,695 | 4/12/2018 |
4.7.2 | 2,304 | 4/8/2018 |
4.7.0.92 | 1,896 | 3/28/2018 |
4.7.0.89 | 2,967 | 1/31/2018 |
4.6.1.82 | 4,193 | 1/4/2018 |
4.6.0.80 | 1,907 | 1/4/2018 |
4.5.0.76 | 1,902 | 12/20/2017 |
4.4.2.75 | 1,735 | 12/18/2017 |
4.4.1.71 | 2,823 | 12/15/2017 |
4.4.0.68 | 1,837 | 12/15/2017 |
4.4.0.66 | 2,003 | 12/13/2017 |
4.3.1 | 1,976 | 12/12/2017 |
4.2.0 | 2,151 | 12/6/2017 |
4.1.0 | 2,363 | 10/26/2017 |
4.1.0-preview-50 | 1,547 | 10/18/2017 |
4.0.48 | 1,927 | 10/17/2017 |
4.0.46 | 1,834 | 10/12/2017 |
4.0.45 | 1,894 | 10/12/2017 |
4.0.44 | 1,903 | 10/12/2017 |
4.0.0-alpha-43 | 1,578 | 10/12/2017 |
4.0.0-alpha-42 | 1,578 | 10/12/2017 |
4.0.0-alpha-41 | 1,622 | 10/11/2017 |
4.0.0-alpha-39 | 1,642 | 9/29/2017 |
4.0.0-alpha-36 | 1,599 | 9/28/2017 |
4.0.0-alpha-35 | 1,544 | 9/28/2017 |
3.3.37 | 2,611 | 9/29/2017 |
3.3.34 | 1,737 | 9/28/2017 |
3.3.32 | 1,764 | 9/28/2017 |
3.3.31 | 1,743 | 9/28/2017 |
3.3.29 | 1,894 | 9/25/2017 |
3.3.28 | 1,723 | 9/25/2017 |
3.3.26 | 1,763 | 9/21/2017 |
3.3.24 | 4,480 | 9/6/2017 |
3.3.17 | 2,063 | 8/8/2017 |
3.3.16 | 1,753 | 8/8/2017 |
3.2.14 | 1,771 | 7/31/2017 |
3.2.13 | 1,719 | 7/31/2017 |
3.2.12 | 1,605 | 7/31/2017 |
3.2.11 | 1,579 | 7/31/2017 |
3.2.9 | 1,618 | 7/31/2017 |
3.2.6 | 1,709 | 7/14/2017 |
3.2.5 | 1,748 | 7/10/2017 |
3.2.4 | 1,639 | 7/10/2017 |
3.2.3 | 1,665 | 7/5/2017 |
3.2.0 | 2,421 | 3/28/2017 |
3.1.2 | 1,723 | 3/21/2017 |
3.1.1 | 1,678 | 3/21/2017 |
3.1.0 | 1,667 | 3/21/2017 |
3.0.1702.1002 | 1,777 | 2/10/2017 |
3.0.1702.1001 | 1,767 | 2/10/2017 |
3.0.1701.3101 | 1,760 | 1/31/2017 |
3.0.1701.1901 | 1,710 | 1/19/2017 |
3.0.1701.1801 | 1,673 | 1/18/2017 |
3.0.1701.1701 | 1,707 | 1/17/2017 |
3.0.1701.1604 | 1,680 | 1/16/2017 |
3.0.1 | 4,016 | 10/26/2016 |
3.0.0 | 1,641 | 10/26/2016 |
2.0.282 | 1,684 | 10/7/2016 |
2.0.281 | 1,549 | 10/5/2016 |
2.0.279 | 1,528 | 10/4/2016 |
2.0.278 | 1,580 | 10/4/2016 |
2.0.277 | 1,631 | 10/4/2016 |
2.0.258-alpha | 1,366 | 8/30/2016 |
2.0.257-alpha | 1,395 | 8/30/2016 |
2.0.256-alpha | 1,392 | 8/30/2016 |
2.0.254-alpha | 1,413 | 8/30/2016 |
2.0.253-alpha | 1,418 | 8/30/2016 |
1.3.0.271 | 1,588 | 9/22/2016 |
1.3.0.270 | 1,580 | 9/22/2016 |
1.3.0.269 | 4,871 | 9/22/2016 |
1.3.0.244 | 2,730 | 8/23/2016 |
1.3.0.242 | 1,624 | 8/22/2016 |
1.2.0.241 | 1,957 | 7/28/2016 |
1.2.0.125 | 3,820 | 6/14/2016 |
1.2.0.120 | 1,846 | 6/9/2016 |
1.2.0.115 | 1,659 | 6/7/2016 |
1.2.0.114 | 1,678 | 6/7/2016 |
1.2.0.113 | 1,633 | 6/7/2016 |
1.2.0.100 | 1,644 | 6/3/2016 |
1.2.0 | 2,524 | 1/18/2016 |
1.1.2 | 1,792 | 1/4/2016 |
1.1.1 | 1,677 | 1/4/2016 |
1.1.0 | 1,668 | 12/18/2015 |
1.0.4 | 1,799 | 11/20/2015 |
1.0.3 | 1,736 | 11/18/2015 |
1.0.2 | 1,740 | 9/16/2015 |
1.0.1 | 1,721 | 9/12/2015 |
1.0.0 | 2,026 | 9/12/2015 |