Beispiel #1
0
        private void GetChildKeys_CanChainEmptyKeys()
        {
            var input = new Dictionary <string, string>()
            {
            };

            for (int i = 0; i < 1000; i++)
            {
                input.Add(new string(' ', i), string.Empty);
            }

            IConfigurationRoot configurationRoot = new ConfigurationBuilder()
                                                   .Add(new MemoryConfigurationSource
            {
                InitialData = input
            })
                                                   .Build();

            var chainedConfigurationSource = new ChainedConfigurationSource
            {
                Configuration = configurationRoot,
                ShouldDisposeConfiguration = false,
            };

            var chainedConfiguration       = new ChainedConfigurationProvider(chainedConfigurationSource);
            IEnumerable <string> childKeys = chainedConfiguration.GetChildKeys(new string[0], null);

            Assert.Equal(1000, childKeys.Count());
            Assert.Equal(string.Empty, childKeys.First());
            Assert.Equal(999, childKeys.Last().Length);
        }
        private static IConfigurationProvider GetChainedConfiguration(string jsonSettings, string[] args)
        {
            ConfigurationBuilder configBuilder = new ConfigurationBuilder();

            configBuilder.Add(new JsonConfigurationSource {
                Path           = jsonSettings,
                Optional       = false,
                FileProvider   = new PhysicalFileProvider(Path.GetDirectoryName(Assembly.GetEntryAssembly().Location)),
                ReloadOnChange = true
            });

            configBuilder.Add(new CommandLineConfigurationSource
            {
                Args = args
            });

            IConfigurationProvider configProvider = new ChainedConfigurationSource
            {
                Configuration = configBuilder.Build()
            }
            .Build(configBuilder);

            configProvider.Load();
            return(configProvider);
        }
        public void ChainedConfiguration_UsingMemoryConfigurationSource_ChainedCouldExposeProvider()
        {
            var chainedConfigurationProvider = new ChainedConfigurationSource
            {
                Configuration = new ConfigurationBuilder()
                                .Add(new MemoryConfigurationSource {
                    InitialData = new Dictionary <string, string>()
                    {
                        { "a:b", "c" }
                    }
                })
                                .Build(),
                ShouldDisposeConfiguration = false,
            }
            .Build(new ConfigurationBuilder()) as ChainedConfigurationProvider;

            Assert.True(chainedConfigurationProvider.TryGet("a:b", out string?value));
            Assert.Equal("c", value);
            Assert.Equal("c", chainedConfigurationProvider.Configuration["a:b"]);

            var configRoot = chainedConfigurationProvider.Configuration as IConfigurationRoot;

            Assert.NotNull(configRoot);
            Assert.Equal(1, configRoot.Providers.Count());
            Assert.IsType <MemoryConfigurationProvider>(configRoot.Providers.First());
        }
        public void ChainedConfiguration_ExposesProvider()
        {
            var providers = new IConfigurationProvider[] {
                new TestConfigurationProvider("foo", "foo-value")
            };
            var chainedConfigurationSource = new ChainedConfigurationSource
            {
                Configuration = new ConfigurationRoot(providers),
                ShouldDisposeConfiguration = false,
            };

            var chainedConfigurationProvider = chainedConfigurationSource
                                               .Build(new ConfigurationBuilder()) as ChainedConfigurationProvider;

            var configRoot = chainedConfigurationProvider.Configuration as IConfigurationRoot;

            Assert.NotNull(configRoot);
            Assert.Equal(providers, configRoot.Providers);
        }