NetLah.Extensions.Configuration
0.2.0-rc1.1
Prefix Reserved
See the version list below for details.
dotnet add package NetLah.Extensions.Configuration --version 0.2.0-rc1.1
NuGet\Install-Package NetLah.Extensions.Configuration -Version 0.2.0-rc1.1
<PackageReference Include="NetLah.Extensions.Configuration" Version="0.2.0-rc1.1" />
paket add NetLah.Extensions.Configuration --version 0.2.0-rc1.1
#r "nuget: NetLah.Extensions.Configuration, 0.2.0-rc1.1"
// Install NetLah.Extensions.Configuration as a Cake Addin #addin nuget:?package=NetLah.Extensions.Configuration&version=0.2.0-rc1.1&prerelease // Install NetLah.Extensions.Configuration as a Cake Tool #tool nuget:?package=NetLah.Extensions.Configuration&version=0.2.0-rc1.1&prerelease
NetLah.Extensions.Configuration - .NET Library
NetLah.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
.
Nuget package
Build Status
Getting started
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
var initConfig = new ConfigurationBuilder().Build();
IConfigurationRoot configuration = new ConfigurationBuilderBuilder()
.WithConfiguration(initConfig)
.WithInMemory(new Dictionary<string, string>{ ["Key:Sub"] = "Value" })
.WithBasePath("C:/App/bin")
.WithCurrentDirectory()
.WithBaseDirectory()
.WithAppSecrets<Program>()
.WithAppSecrets(typeof(Program).Assembly)
.WithAddConfiguration(cb => cb.AddIniFile("appsettings.ini", optional: true, reloadOnChange: true))
.WithEnvironment("Staging")
.WithCommandLines(args)
.Build();
Order of Precedence when Configuring
- Reference
https://devblogs.microsoft.com/premier-developer/order-of-precedence-when-configuring-asp-net-core/
https://docs.microsoft.com/en-us/aspnet/core/fundamentals/configuration/#default-configuration
- Order of Precedence
- Host configuration from environment variables by prefix
DOTNET_
andASPNETCORE_
- Chanined configuration (if any)
- In memory configuration (if any)
- appsettings.json using the JSON configuration provider
- appsettings.EnvironmentName.json using the JSON configuration provider
- Other extra configuration sources
- App secrets when the app runs in the
Development
environment - Environment variables using the Environment Variables configuration provider
- Command-line arguments using the Command-line configuration provider
BasePath of configuration files
The 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();
Environment name
Production
environmentName by default if no host environmentName configuration
ConfigurationBuilderBuilder
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
Specifying environmentName during build configuration
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();
Add extra configuration sources
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();
Helper parse connectionStrings from Configuratin with database provider information
Reference at https://docs.microsoft.com/en-us/aspnet/core/fundamentals/configuration/?#connection-string-prefixes
Support provider
public enum DbProviders
{
Custom,
SQLServer,
PostgreSQL,
MySQL,
}
List of supported provider name
SQLServer
Mssql
SQLAzure
System.Data.SqlClient
Microsoft.Data.SqlClient
MySQL
MySql.Data.MySqlClient
MySqlConnector
PostgreSQL
Npgsql
Postgres
Configuration appsettings.json or environment
"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"
}
Usage:
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") {
...
}
}
Troubleshooting appsettings, configuration and connection strings
Use docker for troubleshooting
Product | Versions Compatible and additional computed target framework versions. |
---|---|
.NET | net5.0 is compatible. net5.0-windows was computed. net6.0 is compatible. net6.0-android was computed. net6.0-ios was computed. net6.0-maccatalyst was computed. net6.0-macos was computed. net6.0-tvos was computed. net6.0-windows was computed. net7.0 was computed. net7.0-android was computed. net7.0-ios was computed. net7.0-maccatalyst was computed. net7.0-macos was computed. net7.0-tvos was computed. net7.0-windows was computed. net8.0 was computed. net8.0-android was computed. net8.0-browser was computed. net8.0-ios was computed. net8.0-maccatalyst was computed. net8.0-macos was computed. net8.0-tvos was computed. net8.0-windows was computed. |
.NET Core | netcoreapp3.0 was computed. netcoreapp3.1 was computed. |
.NET Standard | netstandard2.1 is compatible. |
MonoAndroid | monoandroid was computed. |
MonoMac | monomac was computed. |
MonoTouch | monotouch was computed. |
Tizen | tizen60 was computed. |
Xamarin.iOS | xamarinios was computed. |
Xamarin.Mac | xamarinmac was computed. |
Xamarin.TVOS | xamarintvos was computed. |
Xamarin.WatchOS | xamarinwatchos was computed. |
-
.NETStandard 2.1
- Microsoft.Extensions.Configuration.Binder (>= 3.1.0)
- Microsoft.Extensions.Configuration.CommandLine (>= 3.1.0)
- Microsoft.Extensions.Configuration.EnvironmentVariables (>= 3.1.0)
- Microsoft.Extensions.Configuration.Json (>= 3.1.0)
- Microsoft.Extensions.Configuration.UserSecrets (>= 3.1.0)
- Microsoft.Extensions.Hosting.Abstractions (>= 3.1.0)
- NetLah.Abstractions (>= 0.2.0-rc1.3 && < 1.0.0)
-
net5.0
- Microsoft.Extensions.Configuration.Binder (>= 5.0.0)
- Microsoft.Extensions.Configuration.CommandLine (>= 5.0.0)
- Microsoft.Extensions.Configuration.EnvironmentVariables (>= 5.0.0)
- Microsoft.Extensions.Configuration.Json (>= 5.0.0)
- Microsoft.Extensions.Configuration.UserSecrets (>= 5.0.0)
- Microsoft.Extensions.Hosting.Abstractions (>= 5.0.0)
- NetLah.Abstractions (>= 0.2.0-rc1.3 && < 1.0.0)
-
net6.0
- Microsoft.Extensions.Configuration.Binder (>= 6.0.0-rc.1.21451.13)
- Microsoft.Extensions.Configuration.CommandLine (>= 6.0.0-rc.1.21451.13)
- Microsoft.Extensions.Configuration.EnvironmentVariables (>= 6.0.0-rc.1.21451.13)
- Microsoft.Extensions.Configuration.Json (>= 6.0.0-rc.1.21451.13)
- Microsoft.Extensions.Configuration.UserSecrets (>= 6.0.0-rc.1.21451.13)
- Microsoft.Extensions.Hosting.Abstractions (>= 6.0.0-rc.1.21451.13)
- NetLah.Abstractions (>= 0.2.0-rc1.3 && < 1.0.0)
NuGet packages
This package is not used by any NuGet packages.
GitHub repositories
This package is not used by any popular GitHub repositories.
Version | Downloads | Last updated |
---|---|---|
1.0.0-rc3.3 | 143 | 5/30/2024 |
1.0.0-rc3.2 | 78 | 5/14/2024 |
1.0.0-rc3.1 | 54 | 5/13/2024 |
1.0.0-rc2 | 142 | 3/21/2024 |
1.0.0-rc1 | 405 | 1/12/2024 |
0.2.2 | 1,155 | 1/1/2023 |
0.2.2-rc1 | 761 | 9/14/2022 |
0.2.1 | 1,315 | 11/19/2021 |
0.2.0 | 910 | 11/11/2021 |
0.2.0-rc2 | 36,531 | 10/18/2021 |
0.2.0-rc1.5 | 46,652 | 9/29/2021 |
0.2.0-rc1.1 | 13,159 | 9/16/2021 |
0.1.7 | 82,489 | 8/25/2021 |
0.1.6 | 990 | 8/24/2021 |
0.1.5 | 782 | 8/20/2021 |
0.1.4 | 747 | 8/18/2021 |
0.1.3 | 797 | 8/17/2021 |
0.1.2 | 863 | 8/14/2021 |
0.1.1 | 1,415 | 6/7/2021 |
0.1.0 | 950 | 5/11/2021 |