Reusable classes to build application configuration with environmentName such as ConfigurationBuilderBuilder and CertificateLoader
$ dotnet add package NetLah.Extensions.ConfigurationNetLah.Extensions.Configuration is a library which contains a set of reusable classes for build configuration with environment. These library classes are ConfigurationBuilderBuilder, CertificateLoader and ConnectionStringsHelper.
ConsoleApp
public static void Main(string[] args)
{
var configuration = ConfigurationBuilderBuilder.Create<Program>(args).Build();
var defaultConnectionString = configuration.GetConnectionString("DefaultConnection");
Console.WriteLine($"[TRACE] ConnectionString: {defaultConnectionString}");
}
Full API support
IConfigurationRoot configuration = new ConfigurationBuilderBuilder()
.WithBasePath("C:/App/bin")
.WithCurrentDirectory()
.WithBaseDirectory()
.WithAppSecrets<Program>()
.WithAppSecrets(typeof(Program).Assembly)
.WithEnvironment("Staging")
.WithAddConfiguration(cb => cb.AddIniFile("appsettings.ini", optional: true, reloadOnChange: true))
.WithCommandLines(args)
.Build();
DOTNET_ and ASPNETCORE_Development environmentThe application binary folder is default basePath for appsettings.json, appsettings.Production.json,etc. In case want to change current directory as basePath:
var configuration = new ConfigurationBuilderBuilder()
.WithCurrentDirectory()
.Build();
Production environmentName by default if no host environmentName configurationConfigurationBuilderBuilder will detect EnvironmentName by add configuration environment variables with prefix DOTNET_ and ASPNETCORE_. If no environment variable set, Production will use by default. Example of environment variables:
ASPNETCORE_ENVIRONMENT = Development
DOTNET_ENVIRONMENT = Staging
Sometime, we cannot set the environmentName using environment variable, or we need different environment configuration build lik in unit test project, we can specific the environmentName.
var configuration = ConfigurationBuilderBuilder.Create<ConfigurationBuilderBuilderTest>()
.WithEnvironment("Testing")
.Build();
var configuration = ConfigurationBuilderBuilder.Create<Program>()
.WithAddConfiguration(cb => cb.AddIniFile("appsettings.ini", optional: true, reloadOnChange: true))
.WithAddConfiguration(cb => cb.AddXmlFile("appsettings.xml", optional: true, reloadOnChange: true))
.Build();
Or
var configuration = ConfigurationBuilderBuilder.Create<Program>()
.WithAddConfiguration(
cb => cb.AddIniFile("appsettings.ini", optional: true, reloadOnChange: true)
.AddXmlFile("appsettings.xml", optional: true, reloadOnChange: true)
)
.Build();
Reference at https://docs.microsoft.com/en-us/aspnet/core/fundamentals/configuration/?#connection-string-prefixes
public enum DbProviders
{
Custom,
SQLServer,
PostgreSQL,
MySQL,
}
List of supported provider name
SQLServer
Mssql
SQLAzure
System.Data.SqlClient
MySQL
MySql.Data.MySqlClient
PostgreSQL
"ConnectionStrings": {
"DefaultConnection": "Server=localhost;Database=dbname;Integrated Security=True;",
"DefaultConnection_ProviderName": "System.Data.SqlClient",
"BlogConnection": "AccountEndpoint=https://7d48.documents.azure.com:443/;",
"BlogConnection_ProviderName": "Cosmos1"
}
IConfiguration configuration;
var connStrsHelper = new ConnectionStringsHelper(configuration);
var conn = connStrsHelper["defaultConnection"];
if (conn != null) {
if (conn.Provider == DbProviders.PostgreSQL) {
...
} else if (conn.Provider == DbProviders.MySQL) {
...
} else if (conn.Provider == DbProviders.SQLServer) {
...
} else if (conn.Provider == DbProviders.Custom && conn.Custom == "Cosmos1") {
...
}
}
Use docker for troubleshooting