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 IAppConfigurationSection GetSection(string key) { return(new TenantAppConfigurationSection( root, AppConfigurationPath.Combine(Path, key), tenant )); }
public ConfigurationManagerFixture WithAppSettings <T>(string key, List <T> values) { for (int valueIndex = 0; valueIndex < values.Count; valueIndex++) { string itemKey = AppConfigurationPath.Combine(key, valueIndex.ToString()); T value = values[valueIndex]; appSettings.Add(itemKey, value.ToString()); } return(this); }
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()); }
public void GetSection_WithSection_ReturnsSectionWithKeys(string sectionKey, string key, string value) { var configManagerFixture = new ConfigurationManagerFixture(); int numKeys = new Random().Next(1, 10); for (int keyCt = 1; keyCt <= numKeys; keyCt++) { configManagerFixture.WithAppSetting(AppConfigurationPath.Combine(sectionKey, $"{key}{keyCt}"), $"{value}{keyCt}"); } IConfigurationManager configManager = configManagerFixture.Build(); var appConfig = new AppConfiguration(configManager); IAppConfigurationSection section = appConfig.GetSection(sectionKey); int actual = section.GetChildren().Count(); Assert.That(actual, Is.EqualTo(numKeys)); }
public static T GetValue <T>(this IAppConfigurationRoot configuration, string key, string tenant, T defaultValue) { if (string.IsNullOrEmpty(tenant)) { // Return the "global" setting return(configuration.GetValue(key, defaultValue)); } // Try to get the tenant setting string tenantKey = AppConfigurationPath.Combine(tenant, key); if (configuration.AllKeys.Contains(tenantKey)) { return(configuration.GetValue(tenantKey, defaultValue)); } // Default to "global" setting return(configuration.GetValue(key, defaultValue)); }
public void GetSection_WithSubSection_ReturnsSubSectionWithKeys() { var configManagerFixture = new ConfigurationManagerFixture(); int numKeys = new Random().Next(1, 10); for (int keyCt = 1; keyCt <= numKeys; keyCt++) { configManagerFixture.WithAppSetting(AppConfigurationPath.Combine("Section", $"Key{keyCt}"), $"Value{keyCt}"); configManagerFixture.WithAppSetting(AppConfigurationPath.Combine("Section:SubSection", $"Key{keyCt}"), $"Value{keyCt}"); configManagerFixture.WithAppSetting(AppConfigurationPath.Combine("Section:OtherSubSection", $"Key{keyCt}"), $"Value{keyCt}"); } IConfigurationManager configManager = configManagerFixture.Build(); var appConfig = new AppConfiguration(configManager); IAppConfigurationSection section = appConfig.GetSection("Section"); IAppConfigurationSection subSection = section.GetSection("SubSection"); int actual = subSection.GetChildren().Count(); Assert.That(actual, Is.EqualTo(numKeys)); }