Multiple Source Configuration Reader to manage validated and strongly typed application configuration settings from App.Config, AppSettings.json, and environment variables.
- Typed Configuration Settings.
- Configuration Settings Multiple sources.
- App.Config.
- AppSettings.json.
- AppSettings.Environment.json (environment based).
- Custom json files.
- Environment variables.
- Configuration Settings Validation.
- Type Validation.
- Custom Validation.
- Fallback settings (if settings doesn't exist, check the 'fallback' setting).
- Exception Handling (throwing errors on validation issues is optional).
- Default Values.
- Scope Restrictions - Setting focuses on specific source, if duplicate values are found.
Configuration settings are independent of each other. The settings may be used in a static class, an instance class, or as standalone declarations in a variable.
When initializing a configuration setting, the only required parameter is the Key.
- An optional default value will allow the setting to use an expected value when the key is not found or fails validation.
- An optional scope allows to set the setting to a default scope.
public ConfigSetting(string key, T defaultValue = default, SettingScopes scope = SettingScopes.Any);
- The fallbackSetting allows the setting to try another configuration setting before failing.
- Validation specifies a particular validation function for the value of the setting.
- ThrowOnException specifies whether to throw an error when validation fails. Otherwise, the default value is used.
public ConfigSetting(
string key, T defaultValue, SettingScopes scope, Func<T, bool> validation, bool throwOnException,
ConfigSetting<T> fallbackConfigSetting);
public T Get(bool useBackupSetting = true)
public T Get(ConfigSetting<T> backupConfigSetting)
public bool TryGet(out T val)
public T Convert(string val)
public bool TryConvert(string val, out T newVal)
Get allows retrieval of the setting, respecting the fallback value, but using the default value if the config value and back values fail.
Access the static configuration settings from the Settings class.
var logLevel = Settings.LogLevel.Get();
var path = Settings.DefaultFolder.Get();
var maxRetries = Settings.MaxRetries.Get();
Create a configuration setting and use it immediately.
var val = new ConfigSetting<string>("TestSetting").Get();
Create a configuration setting variable and get the value.
var testSetting = new ConfigSetting<string>("TestSetting", "My Default");
testSetting.Get();
Create an instance of the Settings class and access the setting by the name of the variable.
var settings = new Settings();
var level = settings[nameof(Settings.LogLevel)].ToString();
// Or
var level2 = settings["LogLevel"].ToString();
To ignore any set fallback settings, pass the boolean, false, to the Get method.
var LogLevel = Settings.LogLevel.Get(false);
To override or use a different configuration setting, pass the configuration setting to the Get method. Either use an existing configuration setting or create a new configuration setting.
var Path = Settings.DefaultFolder.Get(Settings.SystemRoot);
var maxRetries = Settings.MaxRetries.Get(new ConfigSetting<int>("OtherRetry", 2));
All configuration settings are declared with data types. In the example code below, each setting specifies the data type of the configuration value.
The datatype is validated for proper conversion. The converted data type is passed to the custom validation routine, if supplied.
The return value with Get, TryGet, Convert, and TryConvert will return the data type that is specified.
new ConfigSetting<int>("IntKey", 1, SettingScopes.Any, number => number > 0);
new ConfigSetting<LogLevels>("LogKey", LogLevels.Information, SettingScopes.Any, level => level != LogLevels.None);
new ConfigSetting<string>("StringKey", string.Empty, SettingScopes.Any, stringSetting => stringSetting != "Foo");
new ConfigSetting<double?>("NullableDoubleKey", 1.5, SettingScopes.Any, number => number.HasValue && number > 0.5);
The most basic configuration setting will be the key itself.
var testSetting = new ConfigSetting<string>("TestSetting");
This setting gets the value of TestSetting "key" from all available configuration sources. If the key is not found, it returns the string None.
var testSetting = new ConfigSetting<string>("TestSetting", "None");
This setting gets the value of AllowedHosts "key" from available AppSettings and Json configuration sources. This setting ignores environment variables. If the key is not found, it returns the string None.
var allowedHosts = new ConfigSetting<string>("AllowedHosts", "None", SettingScopes.AppSettings | SettingScopes.Json);
Both settings validates the value in different ways. Validation functions are typed as specified in the declaration.
var maxRetries = new ConfigSetting<int>("MaxRetries", 2, SettingScopes.Any, i => i > 0);
var folder = new ConfigSetting<string>("Folder", @"C:\Test", SettingScopes.Any, Directory.Exists);
The first setting validates that the configuration value is greater (>) than 0. If the key is not found or the value fails validation,_ it returns the integer 2. The second setting validates that the configuration value exists as a real folder. If the key is not found or the value fails validation,_ it returns the string C:\Test.
public class Settings : SettingsBase<Settings>
{
public static readonly ConfigSetting<LoggingLevels> AppLoggingLevel =
new ConfigSetting<LoggingLevels>("AppLoggingLevel");
public static readonly ConfigSetting<int> MaxRetries =
new ConfigSetting<int>("MaxRetries", 2, SettingScopes.Any, i => i > 0);
}
-
Current
- Remove AppConfig setting from ConfigSetting public interface.
- Add SetAppSettings(NameValueCollection appSettings) to ConfigSetting public interface.
- Add Missing ThrowOnException code.
- Add Action ProcessSettingValue to process data based on found value.
-
1.21.1.1619
- Convert project to .NET Standard 2.0 for greater compatibility.
-
1.21.1.1602
- Allow declaring ConfigSetting without a default value - Uses the data type default.
- Instance dictionary uses the name of the variable instead of the configuration settings Key.
- Update documentation
- Bug Fixes
-
1.21.1.1220
- Add Scoping
- Bug Fixes
-
1.21.1.1116
- Original release
[MIT License]https://github.com/cwinland/AppConfigSettings/blob/master/LICENSE Copyright (c) 2021 Christopher Winland