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;
        }
Ejemplo n.º 2
0
        public IActionResult Index()
        {
            var config = new Configuration();
            config.Add(new EfSettingConfigurationSource(_dbContext));

            ViewBag.TwitterApiKey = config["TwitterApiKey"];
            ViewBag.FacebookApiKey = config["FacebookApiKey"];
            ViewBag.GoogleAnalyticsKey = config["GoogleAnalyticsKey"];

            return View();
        }
        public void CommitOnConfigurationWithZeroCommitableSourceThrows()
        {
            var dict = new Dictionary <string, string>()
            {
                { "Mem:KeyInMem", "MemVal" }
            };
            var memConfigSrc = new MemoryConfigurationSource(dict);
            var config       = new Configuration();

            config.Add(memConfigSrc);

            var exception = Assert.Throws <InvalidOperationException>(() => config.Commit());

            Assert.Equal(1, CountAllEntries(config));
            Assert.Equal(Resources.Error_NoCommitableSource, exception.Message);
        }
Ejemplo n.º 4
0
        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;
        }
Ejemplo n.º 5
0
        public void Main(string[] args)
        {
            var configuration = new Configuration();
            Console.WriteLine("Initial Config Sources: " + configuration.Sources.Count());

            var defaultSettings = new MemoryConfigurationSource();
            defaultSettings.Set("username", "Guest");
            configuration.Add(defaultSettings);
            Console.WriteLine("Added Memory Source. Sources: " + configuration.Sources.Count());

            configuration.AddCommandLine(args);
            Console.WriteLine("Added Command Line Source. Sources: " + configuration.Sources.Count());

            string username = configuration.Get("username");

            Console.WriteLine($"Hello, {username}!");
        }
Ejemplo n.º 6
0
        public void LoadAndCombineKeyValuePairsFromDifferentConfigurationSources()
        {
            // Arrange
            var config = new Configuration();

            // Act
            config.AddIniFile(_iniConfigFilePath);
            config.AddJsonFile(_jsonConfigFilePath);
            config.AddXmlFile(_xmlConfigFilePath);
            var memConfigSrc = new MemoryConfigurationSource(_memConfigContent);

            config.Add(memConfigSrc);

            // Assert
            Assert.Equal(24, CountAllEntries(config));

            Assert.Equal("IniValue1", config.Get("IniKey1"));
            Assert.Equal("IniValue2", config.Get("IniKey2:IniKey3"));
            Assert.Equal("IniValue3", config.Get("IniKey2:IniKey4"));
            Assert.Equal("IniValue4", config.Get("IniKey2:IniKey5:IniKey6"));
            Assert.Equal("IniValue5", config.Get("CommonKey1:CommonKey2:IniKey7"));

            Assert.Equal("JsonValue1", config.Get("JsonKey1"));
            Assert.Equal("JsonValue2", config.Get("Json.Key2:JsonKey3"));
            Assert.Equal("JsonValue3", config.Get("Json.Key2:Json.Key4"));
            Assert.Equal("JsonValue4", config.Get("Json.Key2:JsonKey5:JsonKey6"));
            Assert.Equal("JsonValue5", config.Get("CommonKey1:CommonKey2:JsonKey7"));

            Assert.Equal("XmlValue1", config.Get("XmlKey1"));
            Assert.Equal("XmlValue2", config.Get("XmlKey2:XmlKey3"));
            Assert.Equal("XmlValue3", config.Get("XmlKey2:XmlKey4"));
            Assert.Equal("XmlValue4", config.Get("XmlKey2:XmlKey5:XmlKey6"));
            Assert.Equal("XmlValue5", config.Get("CommonKey1:CommonKey2:XmlKey7"));

            Assert.Equal("MemValue1", config["MemKey1"]);
            Assert.Equal("MemValue2", config["MemKey2:MemKey3"]);
            Assert.Equal("MemValue3", config["MemKey2:MemKey4"]);
            Assert.Equal("MemValue4", config["MemKey2:MemKey5:MemKey6"]);
            Assert.Equal("MemValue5", config["CommonKey1:CommonKey2:MemKey7"]);

            Assert.Equal("MemValue6", config["CommonKey1:CommonKey2:CommonKey3:CommonKey4"]);
        }
Ejemplo n.º 7
0
        public void CanOverrideValuesWithNewConfigurationSource()
        {
            // Arrange
            var config = new Configuration();

            // Act & Assert
            config.AddIniFile(_iniConfigFilePath);
            Assert.Equal("IniValue6", config.Get("CommonKey1:CommonKey2:CommonKey3:CommonKey4"));

            config.AddJsonFile(_jsonConfigFilePath);
            Assert.Equal("JsonValue6", config.Get("CommonKey1:CommonKey2:CommonKey3:CommonKey4"));

            config.AddXmlFile(_xmlConfigFilePath);
            Assert.Equal("XmlValue6", config.Get("CommonKey1:CommonKey2:CommonKey3:CommonKey4"));

            var memConfigSrc = new MemoryConfigurationSource(_memConfigContent);

            config.Add(memConfigSrc);
            Assert.Equal("MemValue6", config.Get("CommonKey1:CommonKey2:CommonKey3:CommonKey4"));
        }
        public void AddCommandLineTest()
        {
            var configuration = new Microsoft.Framework.ConfigurationModel.Configuration();
            configuration.Add(new MemoryConfigurationSource());
            var configurationService = new ConfigurationService(configuration);

            IEnumerable<string> args = new List<string>()
            {
              "build", "/param1=1", "/param2=2 2", "/param3", "3", "posarg1", "--switch1", "true", "/param4", "4 4"
            };

            configurationService.AddCommandLine(configuration, args);

            Assert.AreEqual("build", configuration.Get("arg0"));
            Assert.AreEqual("posarg1", configuration.Get("arg1"));

            Assert.AreEqual("1", configuration.Get("param1"));
            Assert.AreEqual("2 2", configuration.Get("param2"));
            Assert.AreEqual("3", configuration.Get("param3"));
            Assert.AreEqual("4 4", configuration.Get("param4"));

            Assert.AreEqual("true", configuration.Get("switch1"));
        }
Ejemplo n.º 9
0
 public TestConfigurationProvider()
 {
     Configuration = new Configuration();
     Configuration.Add(new MemoryConfigurationSource());
 }