Table of Contents

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.json using ASPNETCORE_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 userSecretsId in appsettings to enable dotnet user-secrets for 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: TokenDecrypt decrypts the value of Token
  • Attribute convention: [ConfigDecrypt("Token")] on any property name decrypts Token into it

Values must be encrypted using string.Encrypt() — the parameterless version — so the same default key is used for both encryption and decryption.

Caveats

  • T cannot be a nested class

  • Config 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.com

  • appsettings.json additionally supports ASPNETCORE_ and DOTNET_ prefixed variables

Tip

Store encrypted values safely in git — they are encrypted with a key only your application knows. See Encryption for details.