コード例 #1
0
        public void LoadKeyValuePairsFromAzureEnvironment()
        {
            var dict = new Hashtable()
                {
                    {"APPSETTING_AppName", "TestAppName"},
                    {"CUSTOMCONNSTR_db1", "CustomConnStr"},
                    {"SQLCONNSTR_db2", "SQLConnStr"},
                    {"MYSQLCONNSTR_db3", "MySQLConnStr"},
                    {"SQLAZURECONNSTR_db4", "SQLAzureConnStr"},
                    {"CommonEnv", "CommonEnvValue"},
                };
            var envConfigSrc = new EnvironmentVariablesConfigurationProvider();

            envConfigSrc.Load(dict);

            string value;
            Assert.Equal("TestAppName", envConfigSrc.Get("APPSETTING_AppName"));
            Assert.False(envConfigSrc.TryGet("AppName", out value));
            Assert.Equal("CustomConnStr", envConfigSrc.Get("Data:db1:ConnectionString"));
            Assert.Equal("SQLConnStr", envConfigSrc.Get("Data:db2:ConnectionString"));
            Assert.Equal("System.Data.SqlClient", envConfigSrc.Get("Data:db2:ProviderName"));
            Assert.Equal("MySQLConnStr", envConfigSrc.Get("Data:db3:ConnectionString"));
            Assert.Equal("MySql.Data.MySqlClient", envConfigSrc.Get("Data:db3:ProviderName"));
            Assert.Equal("SQLAzureConnStr", envConfigSrc.Get("Data:db4:ConnectionString"));
            Assert.Equal("System.Data.SqlClient", envConfigSrc.Get("Data:db4:ProviderName"));
            Assert.Equal("CommonEnvValue", envConfigSrc.Get("CommonEnv"));
        }
コード例 #2
0
        public void LoadKeyValuePairsFromAzureEnvironment()
        {
            var dict = new Hashtable()
            {
                { "APPSETTING_AppName", "TestAppName" },
                { "CUSTOMCONNSTR_db1", "CustomConnStr" },
                { "SQLCONNSTR_db2", "SQLConnStr" },
                { "MYSQLCONNSTR_db3", "MySQLConnStr" },
                { "SQLAZURECONNSTR_db4", "SQLAzureConnStr" },
                { "CommonEnv", "CommonEnvValue" },
            };
            var envConfigSrc = new EnvironmentVariablesConfigurationProvider();

            envConfigSrc.Load(dict);

            string value;

            Assert.Equal("TestAppName", envConfigSrc.Get("APPSETTING_AppName"));
            Assert.False(envConfigSrc.TryGet("AppName", out value));
            Assert.Equal("CustomConnStr", envConfigSrc.Get("ConnectionStrings:db1"));
            Assert.Equal("SQLConnStr", envConfigSrc.Get("ConnectionStrings:db2"));
            Assert.Equal("System.Data.SqlClient", envConfigSrc.Get("ConnectionStrings:db2_ProviderName"));
            Assert.Equal("MySQLConnStr", envConfigSrc.Get("ConnectionStrings:db3"));
            Assert.Equal("MySql.Data.MySqlClient", envConfigSrc.Get("ConnectionStrings:db3_ProviderName"));
            Assert.Equal("SQLAzureConnStr", envConfigSrc.Get("ConnectionStrings:db4"));
            Assert.Equal("System.Data.SqlClient", envConfigSrc.Get("ConnectionStrings:db4_ProviderName"));
            Assert.Equal("CommonEnvValue", envConfigSrc.Get("CommonEnv"));
        }
        protected override (IConfigurationProvider Provider, Action Initializer) LoadThroughProvider(
            TestSection testConfig)
        {
            var values = new List <KeyValuePair <string, string> >();

            SectionToValues(testConfig, "", values);

            var provider = new EnvironmentVariablesConfigurationProvider(null);

            return(provider, () => provider.Load(new Hashtable(values.ToDictionary(e => e.Key, e => e.Value))));
        }
コード例 #4
0
        public void OnlyASinglePrefixIsRemovedFromMatchingKey()
        {
            var dict = new Hashtable()
            {
                { "test__test__ConnectionString", "connection" }
            };
            var envConfigSrc = new EnvironmentVariablesConfigurationProvider("test__");

            envConfigSrc.Load(dict);

            Assert.Equal("connection", envConfigSrc.Get("test:ConnectionString"));
        }
コード例 #5
0
        public void MixingPathSeparatorsInPrefixStillMatchesEnvironmentVariable()
        {
            var dict = new Hashtable()
            {
                { "_____EXPERIMENTAL__data__ConnectionString", "connection" }
            };
            var envConfigSrc = new EnvironmentVariablesConfigurationProvider("::_EXPERIMENTAL:");

            envConfigSrc.Load(dict);

            Assert.Equal("connection", envConfigSrc.Get("data:ConnectionString"));
        }
コード例 #6
0
        public void ReplaceDoubleUnderscoreInEnvironmentVariablesWithDuplicatedPrefix()
        {
            var dict = new Hashtable()
            {
                { "test__test__ConnectionString", "connection" }
            };
            var envConfigSrc = new EnvironmentVariablesConfigurationProvider("test__");

            envConfigSrc.Load(dict);

            Assert.Throws <InvalidOperationException>(() => envConfigSrc.Get("test:ConnectionString"));
        }
コード例 #7
0
        public void ReplaceDoubleUnderscoreInEnvironmentVariablesButNotInAnomalousPrefix()
        {
            var dict = new Hashtable()
            {
                { "_____EXPERIMENTAL__data__ConnectionString", "connection" }
            };
            var envConfigSrc = new EnvironmentVariablesConfigurationProvider("::_EXPERIMENTAL:");

            envConfigSrc.Load(dict);

            Assert.Equal("connection", envConfigSrc.Get("data:ConnectionString"));
        }
コード例 #8
0
        public void ReplaceDoubleUnderscoreInEnvironmentVariablesButNotPrefix()
        {
            var dict = new Hashtable()
            {
                { "test__prefix__with__double__underscores__data__ConnectionString", "connection" }
            };
            var envConfigSrc = new EnvironmentVariablesConfigurationProvider("test__prefix__with__double__underscores__");

            envConfigSrc.Load(dict);

            Assert.Throws <InvalidOperationException>(() => envConfigSrc.Get("data:ConnectionString"));
        }
コード例 #9
0
        public void ReplaceDoubleUnderscoreInEnvironmentVariablesDoubleUnderscorePrefixStillMatches()
        {
            var dict = new Hashtable()
            {
                { "test__prefix__with__double__underscores__data__ConnectionString", "connection" }
            };
            var envConfigSrc = new EnvironmentVariablesConfigurationProvider("test__prefix__with__double__underscores__");

            envConfigSrc.Load(dict);

            Assert.Equal("connection", envConfigSrc.Get("data:ConnectionString"));
        }
コード例 #10
0
        public void PrefixPreventsLoadingSqlConnectionStrings()
        {
            var dict = new Hashtable()
            {
                { "test__test__ConnectionString", "connection" },
                { "SQLCONNSTR_db1", "connStr" }
            };
            var envConfigSrc = new EnvironmentVariablesConfigurationProvider("test:");

            envConfigSrc.Load(dict);

            Assert.Equal("connection", envConfigSrc.Get("test:ConnectionString"));
            Assert.Throws <InvalidOperationException>(() => envConfigSrc.Get("ConnectionStrings:db1_ProviderName"));
        }
コード例 #11
0
        public void LastVariableAddedWhenKeyIsDuplicatedInAzureEnvironment()
        {
            var dict = new Hashtable()
                {
                    {"Data:db2:ConnectionString", "CommonEnvValue"},
                    {"SQLCONNSTR_db2", "SQLConnStr"},
                };
            var envConfigSrc = new EnvironmentVariablesConfigurationProvider();

            envConfigSrc.Load(dict);

            Assert.True(!string.IsNullOrEmpty(envConfigSrc.Get("Data:db2:ConnectionString")));
            Assert.Equal("System.Data.SqlClient", envConfigSrc.Get("Data:db2:ProviderName"));
        }
コード例 #12
0
        public void LastVariableAddedWhenKeyIsDuplicatedInAzureEnvironment()
        {
            var dict = new Hashtable()
            {
                { "ConnectionStrings:db2", "CommonEnvValue" },
                { "SQLCONNSTR_db2", "SQLConnStr" },
            };
            var envConfigSrc = new EnvironmentVariablesConfigurationProvider();

            envConfigSrc.Load(dict);

            Assert.True(!string.IsNullOrEmpty(envConfigSrc.Get("ConnectionStrings:db2")));
            Assert.Equal("System.Data.SqlClient", envConfigSrc.Get("ConnectionStrings:db2_ProviderName"));
        }
コード例 #13
0
        public void ReplaceDoubleUnderscoreInEnvironmentVariables()
        {
            var dict = new Hashtable()
            {
                { "data__ConnectionString", "connection" },
                { "SQLCONNSTR_db1", "connStr" }
            };
            var envConfigSrc = new EnvironmentVariablesConfigurationProvider();

            envConfigSrc.Load(dict);

            Assert.Equal("connection", envConfigSrc.Get("data:ConnectionString"));
            Assert.Equal("System.Data.SqlClient", envConfigSrc.Get("ConnectionStrings:db1_ProviderName"));
        }
コード例 #14
0
        static ConfigurationManager()
        {
            var provider = new EnvironmentVariablesConfigurationProvider();

            provider.Load();
            provider.TryGet("ASPNETCORE_ENVIRONMENT", out string environmentName);

            var builder = new ConfigurationBuilder()
                          .SetBasePath(Directory.GetCurrentDirectory())
                          .AddJsonFile("appsettings.json", optional: true, reloadOnChange: true)
                          .AddJsonFile($"appsettings.{environmentName}.json", optional: true, reloadOnChange: true)
                          .AddEnvironmentVariables();

            Configuration = builder.Build();
        }
コード例 #15
0
        public void LastVariableAddedWhenMultipleEnvironmentVariablesWithSameNameButDifferentCaseExist()
        {
            var dict = new Hashtable()
                {
                    {"CommonEnv", "CommonEnvValue1"},
                    {"commonenv", "commonenvValue2"},
                    {"cOMMonEnv", "commonenvValue3"},
                };
            var envConfigSrc = new EnvironmentVariablesConfigurationProvider();

            envConfigSrc.Load(dict);

            Assert.True(!string.IsNullOrEmpty(envConfigSrc.Get("cOMMonEnv")));
            Assert.True(!string.IsNullOrEmpty(envConfigSrc.Get("CommonEnv")));
        }
コード例 #16
0
        public void IConfigurationProvider_EnvironmentVariablesConfigurationProvider()
        {
            Environment.SetEnvironmentVariable("Name", "SinxHe");
            var source = new EnvironmentVariablesConfigurationSource
            {
                Prefix = null
            };
            var provider = new EnvironmentVariablesConfigurationProvider();

            provider.TryGet("Name", out var name);
            Assert.Null(name);
            provider.Load();
            provider.TryGet("Name", out name);
            Assert.Equal(name, "SinxHe");
        }
コード例 #17
0
        public void LastVariableAddedWhenMultipleEnvironmentVariablesWithSameNameButDifferentCaseExist()
        {
            var dict = new Hashtable()
            {
                { "CommonEnv", "CommonEnvValue1" },
                { "commonenv", "commonenvValue2" },
                { "cOMMonEnv", "commonenvValue3" },
            };
            var envConfigSrc = new EnvironmentVariablesConfigurationProvider();

            envConfigSrc.Load(dict);

            Assert.True(!string.IsNullOrEmpty(envConfigSrc.Get("cOMMonEnv")));
            Assert.True(!string.IsNullOrEmpty(envConfigSrc.Get("CommonEnv")));
        }
コード例 #18
0
        public void LoadKeyValuePairsFromEnvironmentDictionaryWithPrefix()
        {
            var dict = new Hashtable()
                {
                    {"DefaultConnection:ConnectionString", "TestConnectionString"},
                    {"DefaultConnection:Provider", "SqlClient"},
                    {"Inventory:ConnectionString", "AnotherTestConnectionString"},
                    {"Inventory:Provider", "MySql"}
                };
            var envConfigSrc = new EnvironmentVariablesConfigurationProvider("DefaultConnection:");

            envConfigSrc.Load(dict);

            Assert.Equal("TestConnectionString", envConfigSrc.Get("ConnectionString"));
            Assert.Equal("SqlClient", envConfigSrc.Get("Provider"));
        }
コード例 #19
0
        public void LoadKeyValuePairsFromEnvironmentDictionaryWithPrefix()
        {
            var dict = new Hashtable()
            {
                { "DefaultConnection:ConnectionString", "TestConnectionString" },
                { "DefaultConnection:Provider", "SqlClient" },
                { "Inventory:ConnectionString", "AnotherTestConnectionString" },
                { "Inventory:Provider", "MySql" }
            };
            var envConfigSrc = new EnvironmentVariablesConfigurationProvider("DefaultConnection:");

            envConfigSrc.Load(dict);

            Assert.Equal("TestConnectionString", envConfigSrc.Get("ConnectionString"));
            Assert.Equal("SqlClient", envConfigSrc.Get("Provider"));
        }
コード例 #20
0
        public void LoadKeyValuePairsFromEnvironmentDictionary()
        {
            var dict = new Hashtable()
                {
                    {"DefaultConnection:ConnectionString", "TestConnectionString"},
                    {"DefaultConnection:Provider", "SqlClient"},
                    {"Inventory:ConnectionString", "AnotherTestConnectionString"},
                    {"Inventory:Provider", "MySql"}
                };
            var envConfigSrc = new EnvironmentVariablesConfigurationProvider();

            envConfigSrc.Load(dict);

            Assert.Equal("TestConnectionString", envConfigSrc.Get("defaultconnection:ConnectionString"));
            Assert.Equal("SqlClient", envConfigSrc.Get("DEFAULTCONNECTION:PROVIDER"));
            Assert.Equal("AnotherTestConnectionString", envConfigSrc.Get("Inventory:CONNECTIONSTRING"));
            Assert.Equal("MySql", envConfigSrc.Get("Inventory:Provider"));
        }
コード例 #21
0
        public void LoadKeyValuePairsFromEnvironmentDictionary()
        {
            var dict = new Hashtable()
            {
                { "DefaultConnection:ConnectionString", "TestConnectionString" },
                { "DefaultConnection:Provider", "SqlClient" },
                { "Inventory:ConnectionString", "AnotherTestConnectionString" },
                { "Inventory:Provider", "MySql" }
            };
            var envConfigSrc = new EnvironmentVariablesConfigurationProvider(null);

            envConfigSrc.Load(dict);

            Assert.Equal("TestConnectionString", envConfigSrc.Get("defaultconnection:ConnectionString"));
            Assert.Equal("SqlClient", envConfigSrc.Get("DEFAULTCONNECTION:PROVIDER"));
            Assert.Equal("AnotherTestConnectionString", envConfigSrc.Get("Inventory:CONNECTIONSTRING"));
            Assert.Equal("MySql", envConfigSrc.Get("Inventory:Provider"));
        }
コード例 #22
0
        static ConfigurationManager()
        {
            var provider = new EnvironmentVariablesConfigurationProvider();

            provider.Load();
            provider.TryGet("ASPNETCORE_ENVIRONMENT", out string environmentName);

            //使用reloadOnChange为true会造成有线程监听配置文件   可以考虑使用依赖注入配置文件或者改为false
            //https://blog.csdn.net/hiliqi/article/details/80953502
            var builder = new ConfigurationBuilder()
                          .SetBasePath(Directory.GetCurrentDirectory())
                          .AddJsonFile("appsettings.json", optional: true, reloadOnChange: false)
                          .AddJsonFile($"appsettings.{environmentName}.json", optional: true, reloadOnChange: false)
                          .AddJsonFile("host.json", optional: true, reloadOnChange: false)
                          .AddJsonFile($"host.{environmentName}.json", optional: true, reloadOnChange: false)
                          .AddEnvironmentVariables();

            Configuration = builder.Build();
        }
コード例 #23
0
        public void LoadKeyValuePairsFromAzureEnvironment()
        {
            var dict = new Hashtable()
            {
                { "APPSETTING_AppName", "TestAppName" },
                { "CUSTOMCONNSTR_db1", "CustomConnStr" },
                { "SQLCONNSTR_db2", "SQLConnStr" },
                { "MYSQLCONNSTR_db3", "MySQLConnStr" },
                { "SQLAZURECONNSTR_db4", "SQLAzureConnStr" },
                { "CommonEnv", "CommonEnvValue" },
                { "APIHUBCONNSTR_apihub", "ApiHubConnStr" },
                { "DOCDBCONNSTR_cosmos", "CosmosDbConnStr" },
                { "EVENTHUBCONNSTR_eventhub", "EventHubConnStr" },
                { "NOTIFICATIONHUBCONNSTR_notificationhub", "NotificationHubConnStr" },
                { "POSTGRESQLCONNSTR_db5", "PostgresqlConnStr" },
                { "REDISCACHECONNSTR_cache", "RedisConnStr" },
                { "SERVICEBUSCONNSTR_servicebus", "ServiceBusConnStr" },
            };
            var envConfigSrc = new EnvironmentVariablesConfigurationProvider();

            envConfigSrc.Load(dict);

            string value;

            Assert.Equal("TestAppName", envConfigSrc.Get("APPSETTING_AppName"));
            Assert.False(envConfigSrc.TryGet("AppName", out value));
            Assert.Equal("CustomConnStr", envConfigSrc.Get("ConnectionStrings:db1"));
            Assert.Equal("SQLConnStr", envConfigSrc.Get("ConnectionStrings:db2"));
            Assert.Equal("System.Data.SqlClient", envConfigSrc.Get("ConnectionStrings:db2_ProviderName"));
            Assert.Equal("MySQLConnStr", envConfigSrc.Get("ConnectionStrings:db3"));
            Assert.Equal("MySql.Data.MySqlClient", envConfigSrc.Get("ConnectionStrings:db3_ProviderName"));
            Assert.Equal("SQLAzureConnStr", envConfigSrc.Get("ConnectionStrings:db4"));
            Assert.Equal("System.Data.SqlClient", envConfigSrc.Get("ConnectionStrings:db4_ProviderName"));
            Assert.Equal("CommonEnvValue", envConfigSrc.Get("CommonEnv"));
            Assert.Equal("ApiHubConnStr", envConfigSrc.Get("ConnectionStrings:apihub"));
            Assert.Equal("CosmosDbConnStr", envConfigSrc.Get("ConnectionStrings:cosmos"));
            Assert.Equal("EventHubConnStr", envConfigSrc.Get("ConnectionStrings:eventhub"));
            Assert.Equal("NotificationHubConnStr", envConfigSrc.Get("ConnectionStrings:notificationhub"));
            Assert.Equal("PostgresqlConnStr", envConfigSrc.Get("ConnectionStrings:db5"));
            Assert.Equal("RedisConnStr", envConfigSrc.Get("ConnectionStrings:cache"));
            Assert.Equal("ServiceBusConnStr", envConfigSrc.Get("ConnectionStrings:servicebus"));
        }
コード例 #24
0
        public void OnlyEnvironmentVariablesMatchingTheGivenPrefixAreIncluded()
        {
            var dict = new Hashtable()
            {
                { "projectA__section1__project", "A" },
                { "projectA__section1__projectA", "true" },
                { "projectB__section1__project", "B" },
                { "projectB__section1__projectB", "true" },
                { "section1__project", "unknown" },
                { "section1__noProject", "true" }
            };
            var envConfigSrc = new EnvironmentVariablesConfigurationProvider("projectB__");

            envConfigSrc.Load(dict);

            Assert.Equal("B", envConfigSrc.Get("section1:project"));
            Assert.Equal("true", envConfigSrc.Get("section1:projectB"));
            Assert.Throws <InvalidOperationException>(() => envConfigSrc.Get("section1:projectA"));
            Assert.Throws <InvalidOperationException>(() => envConfigSrc.Get("section1:noProject"));
        }
コード例 #25
0
        public void LoadKeyValuePairsFromAzureEnvironmentWithPrefix()
        {
            var dict = new Hashtable()
            {
                { "CUSTOMCONNSTR_db1", "CustomConnStr" },
                { "SQLCONNSTR_db2", "SQLConnStr" },
                { "MYSQLCONNSTR_db3", "MySQLConnStr" },
                { "SQLAZURECONNSTR_db4", "SQLAzureConnStr" },
                { "CommonEnv", "CommonEnvValue" },
            };
            var envConfigSrc = new EnvironmentVariablesConfigurationProvider("ConnectionStrings:");

            envConfigSrc.Load(dict);

            Assert.Equal("CustomConnStr", envConfigSrc.Get("db1"));
            Assert.Equal("SQLConnStr", envConfigSrc.Get("db2"));
            Assert.Equal("System.Data.SqlClient", envConfigSrc.Get("db2_ProviderName"));
            Assert.Equal("MySQLConnStr", envConfigSrc.Get("db3"));
            Assert.Equal("MySql.Data.MySqlClient", envConfigSrc.Get("db3_ProviderName"));
            Assert.Equal("SQLAzureConnStr", envConfigSrc.Get("db4"));
            Assert.Equal("System.Data.SqlClient", envConfigSrc.Get("db4_ProviderName"));
        }
コード例 #26
0
        public void ReplaceDoubleUnderscoreInEnvironmentVariables()
        {
            var dict = new Hashtable()
                {
                    {"data__ConnectionString", "connection"},
                    {"SQLCONNSTR__db1", "connStr"}
                };
            var envConfigSrc = new EnvironmentVariablesConfigurationProvider();

            envConfigSrc.Load(dict);

            Assert.Equal("connection", envConfigSrc.Get("data:ConnectionString"));
            Assert.Equal("System.Data.SqlClient", envConfigSrc.Get("Data:_db1:ProviderName"));
        }
コード例 #27
0
        public void LoadKeyValuePairsFromAzureEnvironmentWithPrefix()
        {
            var dict = new Hashtable()
                {
                    {"CUSTOMCONNSTR_db1", "CustomConnStr"},
                    {"SQLCONNSTR_db2", "SQLConnStr"},
                    {"MYSQLCONNSTR_db3", "MySQLConnStr"},
                    {"SQLAZURECONNSTR_db4", "SQLAzureConnStr"},
                    {"CommonEnv", "CommonEnvValue"},
                };
            var envConfigSrc = new EnvironmentVariablesConfigurationProvider("Data:");

            envConfigSrc.Load(dict);

            Assert.Equal("CustomConnStr", envConfigSrc.Get("db1:ConnectionString"));
            Assert.Equal("SQLConnStr", envConfigSrc.Get("db2:ConnectionString"));
            Assert.Equal("System.Data.SqlClient", envConfigSrc.Get("db2:ProviderName"));
            Assert.Equal("MySQLConnStr", envConfigSrc.Get("db3:ConnectionString"));
            Assert.Equal("MySql.Data.MySqlClient", envConfigSrc.Get("db3:ProviderName"));
            Assert.Equal("SQLAzureConnStr", envConfigSrc.Get("db4:ConnectionString"));
            Assert.Equal("System.Data.SqlClient", envConfigSrc.Get("db4:ProviderName"));
        }