Config
Description
Abstract base class for loading typed configuration from JSON, XML or .config files. Singleton per type — loaded once at first access, thread-safe.
Features
- Auto-discovery: files are located by class name, searched in
~/,~/Configs/and~/Configurations/— file and class names are matched case-insensitively - Transformations: environment-specific overrides via
ClassName.Environment.jsonusingASPNETCORE_ENVIRONMENT - Auto-decryption: encrypted properties are decrypted at startup via naming convention or attribute
- Enum support: public enum fields are automatically mapped from configuration values
- appSettings support: config can also live as a named section inside
appsettings.json - User secrets support: set
userSecretsIdin appsettings to enabledotnet user-secretsfor local development
Behavior
Config is loaded once on first access of Current and cached for the application lifetime. Transformations are applied on top of the base file — only properties defined in the transformation file are overridden.
Decryption happens automatically at startup for properties matching either convention:
- Naming convention:
TokenDecryptdecrypts the value ofToken - Attribute convention:
[ConfigDecrypt("Token")]on any property name decryptsTokeninto it
Values must be encrypted using string.Encrypt() — the parameterless version — so the same default key is used for both encryption and decryption.
Caveats
Tcannot be a nested classConfig file must exist and cannot be empty
Decryption failures are logged as warnings and app continues
Custom config files (non-appsettings) only support
APP_prefixed environment variables for overriding values — e.g.APP_PokemonConfig__Url=https://prod.api.comappsettings.jsonadditionally supportsASPNETCORE_andDOTNET_prefixed variables
Tip
Store encrypted values safely in git — they are encrypted with a key only your application knows. See Encryption for details.