public TenantAppConfigurationSection(IAppConfigurationRoot root, string key, string tenant) { if (string.IsNullOrEmpty(tenant)) { throw new ArgumentException("Cannot be null or empty.", nameof(tenant)); } this.root = root; this.tenant = tenant; string tenantKey, globalKey; if (tenant == key) { tenantKey = tenant; globalKey = null; } else if (key.StartsWith($"{tenant}{AppConfigurationPath.KeyDelimiter}", StringComparison.OrdinalIgnoreCase)) { tenantKey = key; globalKey = key.Substring(tenant.Length + 1); } else { tenantKey = AppConfigurationPath.Combine(tenant, key); globalKey = key; } tenantSection = root.GetSection(tenantKey); globalSection = globalKey == null ? null : root.GetSection(globalKey); Path = tenantKey; Value = tenantSection.Exists() ? tenantSection.Value : globalSection?.Value; }
public static IAppConfigurationSection GetSection(this IAppConfigurationRoot appConfig, string key, string tenant) { if (string.IsNullOrEmpty(tenant)) { // Return the "global" section return(appConfig.GetSection(key)); } return(new TenantAppConfigurationSection(appConfig, key, tenant)); }
public IEnumerable <IAppConfigurationSection> GetChildren() { // We will loop through all of the global children and see if there is a tenant equivalent var children = new List <IAppConfigurationSection>(); var globalChildren = globalSection == null ? root.GetChildren() : globalSection.GetChildren(); foreach (IAppConfigurationSection globalChild in globalChildren) { if (globalChild.Path.Equals(tenant, StringComparison.OrdinalIgnoreCase)) { continue; } string tenantKey = AppConfigurationPath.Combine(tenant, globalChild.Path).ToLower(); // Tenant settings take precedence over global settings if (root.AllKeys.Contains(tenantKey)) { var tenantChild = root.GetSection(tenantKey); children.Add(tenantChild); } else { var tenantChild = new TenantAppConfigurationSection( root, AppConfigurationPath.Combine(tenant, globalChild.Path), tenant ); children.Add(tenantChild); } } return(children.AsReadOnly()); }