.NET › Configuration & Options
How would you design configuration for a multi-tenant application where each tenant needs different settings resolved at runtime?
AdvancedAnswer
Common approaches include: a custom configuration provider that loads and merges tenant-specific overrides on top of global defaults at request time (using the current tenant context, often resolved via IHttpContextAccessor or a middleware-set value), or a tenant-aware options resolution pattern (a custom ITenantOptionsProvider service) that layers a base IOptions<T> with tenant-specific overrides fetched from a database or cache -- since the standard Options pattern's IOptionsSnapshot is scoped per-request but not inherently tenant-aware, additional logic is needed to select the right values for the current tenant.
public class TenantSettingsResolver {
private readonly IOptionsMonitor<Dictionary<string, TenantSettings>> _allTenantSettings;
public TenantSettings GetForCurrentTenant(string tenantId) {
return _allTenantSettings.CurrentValue.TryGetValue(tenantId, out var settings)
? settings
: _defaultSettings;
}
}Real-world example A B2B SaaS platform resolves each tenant's custom branding colors, feature flags, and rate limits through a tenant-aware settings resolver that layers tenant-specific database overrides on top of a global default configuration baseline.