Example #1
0
        static void Main(string[] args)
        {
            var builder = new ConfigurationBuilder()
                          .SetBasePath(Directory.GetCurrentDirectory())
                          .AddJsonFile("appsettings.json")
                          .AddEnvironmentVariables();

            var configuration = builder.Build();

            JsonConfigurationProvider jsonConfig = configuration.Providers.FirstOrDefault() as JsonConfigurationProvider;
            EnvironmentVariablesConfigurationProvider envConfig = configuration.Providers.LastOrDefault() as EnvironmentVariablesConfigurationProvider;

            while (true)
            {
                string connStr = string.Empty;

                if (envConfig.TryGet("TestConnection", out connStr))
                {
                    TestConnection(connStr);
                    return;
                }

                int i = 1;

                while (jsonConfig.TryGet("TestConnection" + i, out connStr))
                {
                    i++;

                    LogToConsole("--------------------");
                    LogToConsole("Will test " + connStr);
                    TestConnection(connStr);
                }

                LogToConsole("--------------------");
                LogToConsole("And now ENV variables");

                i = 1;

                while (envConfig.TryGet("TestConnection" + i, out connStr))
                {
                    i++;

                    LogToConsole("--------------------");
                    LogToConsole("Will test " + connStr);

                    TestConnection(connStr);
                }


                Thread.Sleep(new TimeSpan(0, 1, 0));
            }


            //Console.WriteLine("We're done. Press any key to quit.");
            //Console.ReadKey();
        }
Example #2
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");
        }
        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"));
        }
        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"));
        }
Example #5
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();
        }
Example #6
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();
        }
Example #7
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"));
        }