Пример #1
0
        static void Main(string[] args)
        {
            //Load configuration files - configuration files are loaded general -> specific.
            //Files that are loaded later overwrite the ones that are loaded earlier
            ConfigurationManager.AddConfiguration("Global_Config_Standard.txt");           //Layer 0
            ConfigurationManager.AddConfiguration("Global_Config_ProjectSpecific.txt");    //Layer 1
            ConfigurationManager.AddConfiguration("Global_Config_ExperimentSpecifix.txt"); //Layer 2

            //Configure custom model
            var config = ConfigurationManager.Configure <CustomConfig>();

            //When using Configure method options can be access via property accessors example
            Console.WriteLine($"Number of systems: {config.NumberOfSystems}");


            Console.WriteLine("Get all loaded configuration entries");
            foreach (var entry in ConfigurationManager.GetAll())
            {
                Console.WriteLine($"{entry.Key}: {entry.Value}");
            }

            Console.WriteLine("Get specific entry as string");
            Console.WriteLine($"OrdersPerHour: {ConfigurationManager.GetValueAsString("OrdersPerHour")}");
            Console.WriteLine($"OrderLinesPerOrder: {ConfigurationManager.GetValueAsString("OrderLinesPerOrder")}");

            Console.WriteLine("Get specific entry casted");
            Console.WriteLine($"OrdersPerHour: {ConfigurationManager.GetValue<int>("OrdersPerHour")}");

            //Throws exception if wrong cast
            //Console.WriteLine($"{ConfigurationManager.GetValue<bool>("OrdersPerHour")}");
            Console.Read();
        }
Пример #2
0
        public void CanChainConfiguration()
        {
            // Arrange
            var dic1 = new Dictionary <string, string>()
            {
                { "Mem1:KeyInMem1", "ValueInMem1" }
            };
            var dic2 = new Dictionary <string, string>()
            {
                { "Mem2:KeyInMem2", "ValueInMem2" }
            };
            var dic3 = new Dictionary <string, string>()
            {
                { "Mem3:KeyInMem3", "ValueInMem3" }
            };
            var memConfigSrc1 = new MemoryConfigurationSource {
                InitialData = dic1
            };
            var memConfigSrc2 = new MemoryConfigurationSource {
                InitialData = dic2
            };
            var memConfigSrc3 = new MemoryConfigurationSource {
                InitialData = dic3
            };

            var config = new ConfigurationManager();
            IConfigurationBuilder configurationBuilder = config;

            // Act
            configurationBuilder.Add(memConfigSrc1);
            configurationBuilder.Add(memConfigSrc2);
            configurationBuilder.Add(memConfigSrc3);

            var chained = new ConfigurationManager();

            chained.AddConfiguration(config);
            var memVal1 = chained["mem1:keyinmem1"];
            var memVal2 = chained["Mem2:KeyInMem2"];
            var memVal3 = chained["MEM3:KEYINMEM3"];

            // Assert

            Assert.Equal("ValueInMem1", memVal1);
            Assert.Equal("ValueInMem2", memVal2);
            Assert.Equal("ValueInMem3", memVal3);

            Assert.Null(chained["NotExist"]);
        }
Пример #3
0
        public void ChainedConfigurationIsDisposedOnDispose(bool shouldDispose)
        {
            var provider      = new DisposableTestConfigurationProvider("foo", "foo-value");
            var chainedConfig = new ConfigurationRoot(new IConfigurationProvider[] {
                provider
            });

            var config = new ConfigurationManager();

            config.AddConfiguration(chainedConfig, shouldDisposeConfiguration: shouldDispose);

            Assert.False(provider.IsDisposed);

            config.Dispose();

            Assert.Equal(shouldDispose, provider.IsDisposed);
        }
Пример #4
0
        public void ChainedAsEnumerateFlattensIntoDictionaryTest(bool removePath)
        {
            // Arrange
            var dic1 = new Dictionary <string, string>()
            {
                { "Mem1", "Value1" },
                { "Mem1:", "NoKeyValue1" },
                { "Mem1:KeyInMem1", "ValueInMem1" },
                { "Mem1:KeyInMem1:Deep1", "ValueDeep1" }
            };
            var dic2 = new Dictionary <string, string>()
            {
                { "Mem2", "Value2" },
                { "Mem2:", "NoKeyValue2" },
                { "Mem2:KeyInMem2", "ValueInMem2" },
                { "Mem2:KeyInMem2:Deep2", "ValueDeep2" }
            };
            var dic3 = new Dictionary <string, string>()
            {
                { "Mem3", "Value3" },
                { "Mem3:", "NoKeyValue3" },
                { "Mem3:KeyInMem3", "ValueInMem3" },
                { "Mem3:KeyInMem3:Deep3", "ValueDeep3" }
            };
            var memConfigSrc1 = new MemoryConfigurationSource {
                InitialData = dic1
            };
            var memConfigSrc2 = new MemoryConfigurationSource {
                InitialData = dic2
            };
            var memConfigSrc3 = new MemoryConfigurationSource {
                InitialData = dic3
            };

            var config1 = new ConfigurationManager();
            IConfigurationBuilder configurationBuilder = config1;

            // Act
            configurationBuilder.Add(memConfigSrc1);
            configurationBuilder.Add(memConfigSrc2);

            var config2 = new ConfigurationManager();

            config2
            .AddConfiguration(config1)
            .Add(memConfigSrc3);

            var dict = config2.AsEnumerable(makePathsRelative: removePath).ToDictionary(k => k.Key, v => v.Value);

            // Assert
            Assert.Equal("Value1", dict["Mem1"]);
            Assert.Equal("NoKeyValue1", dict["Mem1:"]);
            Assert.Equal("ValueDeep1", dict["Mem1:KeyInMem1:Deep1"]);
            Assert.Equal("ValueInMem2", dict["Mem2:KeyInMem2"]);
            Assert.Equal("Value2", dict["Mem2"]);
            Assert.Equal("NoKeyValue2", dict["Mem2:"]);
            Assert.Equal("ValueDeep2", dict["Mem2:KeyInMem2:Deep2"]);
            Assert.Equal("Value3", dict["Mem3"]);
            Assert.Equal("NoKeyValue3", dict["Mem3:"]);
            Assert.Equal("ValueInMem3", dict["Mem3:KeyInMem3"]);
            Assert.Equal("ValueDeep3", dict["Mem3:KeyInMem3:Deep3"]);
        }