public static IConfigurationSourceRoot RegisterConfiguration([NotNull] this Startup startup, [NotNull] string toolsDirectory, [NotNull] string projectDirectory, ConfigurationOptions options) { var configuration = new Microsoft.Framework.ConfigurationModel.Configuration(); configuration.Add(new MemoryConfigurationSource()); configuration.Set(Constants.Configuration.ToolsDirectory, toolsDirectory); configuration.Set(Constants.Configuration.ProjectDirectory, projectDirectory); configuration.Set(Constants.Configuration.SystemConfigFileName, "scconfig.json"); var configurationService = new ConfigurationService(configuration); if ((options & ConfigurationOptions.DoNotLoadConfig) != ConfigurationOptions.DoNotLoadConfig) { try { configurationService.Load(options); } catch (Exception ex) { Console.WriteLine(ex.Message); Console.WriteLine(ex.StackTrace); return null; } } return configuration; }
public void SettingValueUpdatesAllConfigurationSources() { // Arrange var dict = new Dictionary <string, string>() { { "Key1", "Value1" }, { "Key2", "Value2" } }; var memConfigSrc1 = new MemoryConfigurationSource(dict); var memConfigSrc2 = new MemoryConfigurationSource(dict); var memConfigSrc3 = new MemoryConfigurationSource(dict); var config = new Configuration(); config.AddLoadedSource(memConfigSrc1); config.AddLoadedSource(memConfigSrc2); config.AddLoadedSource(memConfigSrc3); // Act config.Set("Key1", "NewValue1"); config["Key2"] = "NewValue2"; // Assert Assert.Equal(6, CountAllEntries(config)); Assert.Equal("NewValue1", config.Get("Key1")); Assert.Equal("NewValue1", memConfigSrc1.Data["Key1"]); Assert.Equal("NewValue1", memConfigSrc2.Data["Key1"]); Assert.Equal("NewValue1", memConfigSrc3.Data["Key1"]); Assert.Equal("NewValue2", config["Key2"]); Assert.Equal("NewValue2", memConfigSrc1.Data["Key2"]); Assert.Equal("NewValue2", memConfigSrc2.Data["Key2"]); Assert.Equal("NewValue2", memConfigSrc3.Data["Key2"]); }
public int UpdateCount() { var config = new Configuration(); config.Add(new MemoryConfigurationSource()); var KEY = "count"; var count = 1; var value = config.Get(KEY); if (value != null) { count = int.Parse(value); count++; } config.Set(KEY, count.ToString()); return count; }
public void CanCommitChangeBackToLastConfigFile() { // Arrange var config = new Configuration(); config.AddIniFile(_iniConfigFilePath); config.AddJsonFile(_jsonConfigFilePath); config.AddXmlFile(_xmlConfigFilePath); config.Set("CommonKey1:CommonKey2:CommonKey3:CommonKey4", "NewValue"); // Act config.Commit(); // Assert Assert.Equal(_iniConfigFileContent, File.ReadAllText(_iniConfigFilePath)); Assert.Equal(_jsonConfigFileContent, File.ReadAllText(_jsonConfigFilePath)); var updatedXmlContent = _xmlConfigFileContent.Replace("XmlValue6", "NewValue"); Assert.Equal(updatedXmlContent, File.ReadAllText(_xmlConfigFilePath)); }
public void CanSetValuesAndReloadValues() { // Arrange var config = new Configuration(); config.AddIniFile(_iniConfigFilePath); config.AddJsonFile(_jsonConfigFilePath); config.AddXmlFile(_xmlConfigFilePath); // Act & Assert // Set value with Set() method config.Set("CommonKey1:CommonKey2:CommonKey3:CommonKey4", "NewValue"); // All config sources must be updated foreach (var src in config) { Assert.Equal("NewValue", (src as BaseConfigurationSource).Data["CommonKey1:CommonKey2:CommonKey3:CommonKey4"]); } // Recover values by reloading config.Reload(); Assert.Equal("XmlValue6", config.Get("CommonKey1:CommonKey2:CommonKey3:CommonKey4")); // Set value with indexer config["CommonKey1:CommonKey2:CommonKey3:CommonKey4"] = "NewValue"; // All config sources must be updated foreach (var src in config) { Assert.Equal("NewValue", (src as BaseConfigurationSource).Data["CommonKey1:CommonKey2:CommonKey3:CommonKey4"]); } // Recover values by reloading config.Reload(); Assert.Equal("XmlValue6", config["CommonKey1:CommonKey2:CommonKey3:CommonKey4"]); }
public void Set(string key, string value) { _root.Set(_prefix + key, value); }
public void SettingValueUpdatesAllConfigurationSources() { // Arrange var dict = new Dictionary<string, string>() { {"Key1", "Value1"}, {"Key2", "Value2"} }; var memConfigSrc1 = new MemoryConfigurationSource(dict); var memConfigSrc2 = new MemoryConfigurationSource(dict); var memConfigSrc3 = new MemoryConfigurationSource(dict); var config = new Configuration(); config.AddLoadedSource(memConfigSrc1); config.AddLoadedSource(memConfigSrc2); config.AddLoadedSource(memConfigSrc3); // Act config.Set("Key1", "NewValue1"); config["Key2"] = "NewValue2"; // Assert Assert.Equal("NewValue1", config.Get("Key1")); Assert.Equal("NewValue1", memConfigSrc1.Get("Key1")); Assert.Equal("NewValue1", memConfigSrc2.Get("Key1")); Assert.Equal("NewValue1", memConfigSrc3.Get("Key1")); Assert.Equal("NewValue2", config["Key2"]); Assert.Equal("NewValue2", memConfigSrc1.Get("Key2")); Assert.Equal("NewValue2", memConfigSrc2.Get("Key2")); Assert.Equal("NewValue2", memConfigSrc3.Get("Key2")); }