You're Invited:Meet the Socket Team at BlackHat and DEF CON in Las Vegas, Aug 4-6.RSVP
Socket
Book a DemoInstallSign in
Socket

NetLah.Extensions.Configuration

Package Overview
Dependencies
Maintainers
2
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

NetLah.Extensions.Configuration

Reusable classes to build application configuration with environmentName such as ConfigurationBuilderBuilder and CertificateLoader

1.0.0
Source
nugetNuGet
Version published
Maintainers
2
Created
Source

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 ConnectionStringManager.

Nuget package

NuGet

Build Status

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_ and ASPNETCORE_
  • 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();

ConnectionStringManager load from Configuration 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",
    "BlogConnection2_Cosmos": "AccountEndpoint=https://7d48.documents.azure.com:443/;"
  }
}

Basic usage:

IConfiguration configuration;
var connStrManager = new ConnectionStringManager(configuration);
var conn = connStrManager["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") {
        ...
    }
}

Multi connectionNames:

var conn = connStrManager["BlogConnection", "BlogConnection2"];
if (conn != null) {
    if (conn.Provider == DbProviders.Custom && conn.Custom == "Cosmos1") {
        ...
    } else if (conn.Provider == DbProviders.Custom && conn.Custom == "Cosmos") {
        ...
    }
}

Troubleshooting appsettings, configuration and connection strings

Use docker for troubleshooting

https://github.com/NetLah/EchoServiceApi

https://hub.docker.com/r/netlah/echo-service-api

Keywords

appsettings

FAQs

Package last updated on 04 Apr 2025

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts