Config.Net
5.0.2
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
<PackageReference Include="Config.Net" Version="5.0.2" />
paket add Config.Net --version 5.0.2
#r "nuget: Config.Net, 5.0.2"
// 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
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
- Extending Config.Net
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):
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 (10)
Showing the top 5 popular GitHub repositories that depend on Config.Net:
Repository | Stars |
---|---|
srwi/EverythingToolbar
Everything integration for the Windows taskbar.
|
|
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
|
Version | Downloads | Last updated |
---|---|---|
5.3.0-pre.1 | 70 | 11/6/2024 |
5.2.1 | 999 | 11/6/2024 |
5.2.1-pre.1 | 36 | 11/6/2024 |
5.2.0 | 36,285 | 4/2/2024 |
5.1.5 | 87,831 | 11/21/2022 |
5.1.4 | 9,660 | 11/4/2022 |
5.1.3 | 4,544 | 9/20/2022 |
5.1.2 | 3,545 | 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 | 924 | 7/20/2022 |
4.19.0 | 91,380 | 12/9/2021 |
4.18.0 | 878 | 12/9/2021 |
4.17.0 | 5,985 | 9/16/2021 |
4.16.2 | 2,050 | 9/16/2021 |
4.15.0 | 55,649 | 7/29/2020 |
4.14.25 | 1,529 | 7/29/2020 |
4.14.24 | 2,277 | 7/12/2020 |
4.14.23 | 45,752 | 1/16/2020 |
4.14.20 | 3,771 | 1/3/2020 |
4.14.19 | 1,483 | 1/3/2020 |
4.14.16 | 5,806 | 12/10/2019 |
4.14.15 | 1,696 | 12/9/2019 |
4.14.14 | 6,417 | 11/29/2019 |
4.13.7 | 41,806 | 4/23/2019 |
4.13.5 | 1,855 | 4/23/2019 |
4.13.2 | 75,437 | 1/27/2019 |
4.12.0 | 1,713 | 1/23/2019 |
4.11.0 | 18,408 | 10/16/2018 |
4.10.1 | 5,607 | 8/6/2018 |
4.10.0 | 14,575 | 5/29/2018 |
4.9.1 | 2,858 | 4/26/2018 |
4.9.0 | 1,814 | 4/26/2018 |
4.8.0 | 2,031 | 4/18/2018 |
4.7.3 | 2,697 | 4/12/2018 |
4.7.2 | 2,309 | 4/8/2018 |
4.7.0.92 | 1,898 | 3/28/2018 |
4.7.0.89 | 2,969 | 1/31/2018 |
4.6.1.82 | 4,197 | 1/4/2018 |
4.6.0.80 | 1,909 | 1/4/2018 |
4.5.0.76 | 1,904 | 12/20/2017 |
4.4.2.75 | 1,737 | 12/18/2017 |
4.4.1.71 | 2,825 | 12/15/2017 |
4.4.0.68 | 1,841 | 12/15/2017 |
4.4.0.66 | 2,007 | 12/13/2017 |
4.3.1 | 1,978 | 12/12/2017 |
4.2.0 | 2,155 | 12/6/2017 |
4.1.0 | 2,365 | 10/26/2017 |
4.1.0-preview-50 | 1,549 | 10/18/2017 |
4.0.48 | 1,929 | 10/17/2017 |
4.0.46 | 1,836 | 10/12/2017 |
4.0.45 | 1,896 | 10/12/2017 |
4.0.44 | 1,905 | 10/12/2017 |
4.0.0-alpha-43 | 1,580 | 10/12/2017 |
4.0.0-alpha-42 | 1,580 | 10/12/2017 |
4.0.0-alpha-41 | 1,624 | 10/11/2017 |
4.0.0-alpha-39 | 1,644 | 9/29/2017 |
4.0.0-alpha-36 | 1,601 | 9/28/2017 |
4.0.0-alpha-35 | 1,546 | 9/28/2017 |
3.3.37 | 2,615 | 9/29/2017 |
3.3.34 | 1,739 | 9/28/2017 |
3.3.32 | 1,766 | 9/28/2017 |
3.3.31 | 1,745 | 9/28/2017 |
3.3.29 | 1,896 | 9/25/2017 |
3.3.28 | 1,725 | 9/25/2017 |
3.3.26 | 1,765 | 9/21/2017 |
3.3.24 | 4,482 | 9/6/2017 |
3.3.17 | 2,065 | 8/8/2017 |
3.3.16 | 1,755 | 8/8/2017 |
3.2.14 | 1,773 | 7/31/2017 |
3.2.13 | 1,721 | 7/31/2017 |
3.2.12 | 1,607 | 7/31/2017 |
3.2.11 | 1,581 | 7/31/2017 |
3.2.9 | 1,620 | 7/31/2017 |
3.2.6 | 1,711 | 7/14/2017 |
3.2.5 | 1,750 | 7/10/2017 |
3.2.4 | 1,641 | 7/10/2017 |
3.2.3 | 1,667 | 7/5/2017 |
3.2.0 | 2,423 | 3/28/2017 |
3.1.2 | 1,725 | 3/21/2017 |
3.1.1 | 1,680 | 3/21/2017 |
3.1.0 | 1,669 | 3/21/2017 |
3.0.1702.1002 | 1,783 | 2/10/2017 |
3.0.1702.1001 | 1,769 | 2/10/2017 |
3.0.1701.3101 | 1,762 | 1/31/2017 |
3.0.1701.1901 | 1,712 | 1/19/2017 |
3.0.1701.1801 | 1,675 | 1/18/2017 |
3.0.1701.1701 | 1,709 | 1/17/2017 |
3.0.1701.1604 | 1,682 | 1/16/2017 |
3.0.1 | 4,130 | 10/26/2016 |
3.0.0 | 1,643 | 10/26/2016 |
2.0.282 | 1,687 | 10/7/2016 |
2.0.281 | 1,551 | 10/5/2016 |
2.0.279 | 1,530 | 10/4/2016 |
2.0.278 | 1,582 | 10/4/2016 |
2.0.277 | 1,633 | 10/4/2016 |
2.0.258-alpha | 1,368 | 8/30/2016 |
2.0.257-alpha | 1,397 | 8/30/2016 |
2.0.256-alpha | 1,394 | 8/30/2016 |
2.0.254-alpha | 1,415 | 8/30/2016 |
2.0.253-alpha | 1,420 | 8/30/2016 |
1.3.0.271 | 1,591 | 9/22/2016 |
1.3.0.270 | 1,582 | 9/22/2016 |
1.3.0.269 | 4,874 | 9/22/2016 |
1.3.0.244 | 2,732 | 8/23/2016 |
1.3.0.242 | 1,626 | 8/22/2016 |
1.2.0.241 | 1,959 | 7/28/2016 |
1.2.0.125 | 3,823 | 6/14/2016 |
1.2.0.120 | 1,848 | 6/9/2016 |
1.2.0.115 | 1,661 | 6/7/2016 |
1.2.0.114 | 1,680 | 6/7/2016 |
1.2.0.113 | 1,635 | 6/7/2016 |
1.2.0.100 | 1,646 | 6/3/2016 |
1.2.0 | 2,527 | 1/18/2016 |
1.1.2 | 1,795 | 1/4/2016 |
1.1.1 | 1,679 | 1/4/2016 |
1.1.0 | 1,670 | 12/18/2015 |
1.0.4 | 1,801 | 11/20/2015 |
1.0.3 | 1,739 | 11/18/2015 |
1.0.2 | 1,742 | 9/16/2015 |
1.0.1 | 1,723 | 9/12/2015 |
1.0.0 | 2,029 | 9/12/2015 |