ExternalConfigurationProvider is a .NET library for getting services configs from external centralized configuration store.
- Get specified service settings from Consul
- Cache service settings
- Consul
- Store sensitive data (passwords, connection strings) in external store instead of VCS.
- Store all settings in one centralized store. When you change settings of a service you don't need to update config files of all applications which use the service. You just update config in external configuration store.
Install and run Consul following guide.
Add to Consul KV values:
dev/redis/url http://localhost:6379
dev/redis/password StrongPassword
staging/azure/redis/url http://azure.com/redis
staging/azure/redis/password SuperStrongPassword
Register Consul configuration provider service in DI:
using ExternalConfiguration;
// Getting current environment.
// In our exmaple: dev or staging.
var env = ... ;
serviceCollection.AddConsulConfigurationProvider(config =>
{
config.Url = "http://localhost:8500";
config.Environment = env;
});
Use IExternalConfigurationProvider
to get settings in dev
environment:
public class Repository
{
private IRedisClient _redisClient;
public Repository(IExternalConfigurationProvider externalConfigurationProvider)
{
var redisConfig = externalConfigurationProvider.GetServiceSettingsAsync("redis").Result;
_redisClient = new RedisClient(redisConfig["url"], redisConfig["password"]);
// http://localhost:6379 StrongPassword
}
}
In case of hosting
is specified:
var redisConfig = externalConfigurationProvider.GetServiceSettingsAsync("redis", "azure");
var redisClient = new RedisClient(redisConfig["url"], redisConfig["password"]);
// http://azure.com/redis SuperStrongPassword