Priority Order
Understanding configuration priority in AuthScape.
Configuration Priority Order
AuthScape loads configuration from multiple sources. Understanding the priority helps prevent unexpected behavior.
Default Priority (Lowest to Highest)
- appsettings.json - Base configuration
- appsettings.{Environment}.json - Environment-specific
- User Secrets - Development only
- Environment Variables - System/container level
- Command-line Arguments - Highest priority
- Azure Key Vault / AWS Secrets - When configured
Higher priority sources override lower priority sources.
Example
Given these configurations:
appsettings.json:
json
{"AppSettings": {"LogLevel": "Information","MaxRetries": 3}}
appsettings.Production.json:
json
{"AppSettings": {"LogLevel": "Warning"}}
Environment Variable:
text
AppSettings__MaxRetries=5
Result in Production:
json
{"AppSettings": {"LogLevel": "Warning", // From Production.json"MaxRetries": 5 // From Environment Variable}}
How Loading Works
csharp
var builder = WebApplication.CreateBuilder(args);// Default loading order:builder.Configuration.AddJsonFile("appsettings.json", optional: false).AddJsonFile($"appsettings.{env}.json", optional: true).AddUserSecrets<Program>(optional: true) // Development only.AddEnvironmentVariables().AddCommandLine(args);
Custom Priority
Override the default order:
csharp
builder.Configuration.Sources.Clear();builder.Configuration.AddJsonFile("appsettings.json").AddAzureKeyVault(...) // Load early, can be overridden.AddEnvironmentVariables().AddJsonFile($"appsettings.{env}.json"); // Load last, highest priority
Best Practices
- Base settings in appsettings.json - Non-sensitive defaults
- Environment overrides - Logging levels, feature flags
- Secrets in secure stores - API keys, connection strings
- Environment variables for containers - Dynamic configuration
Debugging Configuration
csharp
// Log all configuration sourcesforeach (var source in builder.Configuration.Sources){Console.WriteLine(source.GetType().Name);}// Log specific value and sourcevar value = builder.Configuration["AppSettings:LogLevel"];Console.WriteLine($"LogLevel = {value}");