CatConsult.EnvConfigurationProvider
1.1.0
dotnet add package CatConsult.EnvConfigurationProvider --version 1.1.0
NuGet\Install-Package CatConsult.EnvConfigurationProvider -Version 1.1.0
<PackageReference Include="CatConsult.EnvConfigurationProvider" Version="1.1.0" />
paket add CatConsult.EnvConfigurationProvider --version 1.1.0
#r "nuget: CatConsult.EnvConfigurationProvider, 1.1.0"
// Install CatConsult.EnvConfigurationProvider as a Cake Addin #addin nuget:?package=CatConsult.EnvConfigurationProvider&version=1.1.0 // Install CatConsult.EnvConfigurationProvider as a Cake Tool #tool nuget:?package=CatConsult.EnvConfigurationProvider&version=1.1.0
CatConsult.EnvConfigurationProvider
This repository contains a .NET Configuration Provider
that offers a fluent API for loading and transforming environment variables from the OS and .env
files.
Overview
In many other languages and frameworks, .env
s are text files that contain key-value pairs of configuration data or secrets.
These files are not typically checked in to source control and can vary between developers.
Some tools, such as Docker Compose, can read .env
as well, making the file an enticing location for storing shared configuration
that both Docker and your .NET app can use.
This provider offers the convenience of loading configuration and secrets from .env
files and also from the local operating system.
System variables have a higher precedence than ones found in .env
files, making it easy to override them where needed.
In most production workloads, configuration and secrets should be sourced from more secure and flexible sources, such as Azure Key Vault or AWS Secrets Manager. As such, this provider is intended to be used primarily in development and testing scenarios.
Usage
First, download the provider from NuGet:
dotnet add package CatConsult.EnvConfigurationProvider
Then add the provider using the AddEnvs()
extension method:
if (builder.Environment.IsDevelopment())
{
builder.Configuration
.AddEnvs(config => config
.AddRequiredEnv("GREETING_FORMAT", "Greeting:Format")
.AddOptionalEnv("GREETING_NAME", "Greeting:Name", "EnvConfigurationProvider")
.AddCustomMapper(envs => new ConfigurationEntry(
"ConnectionStrings:DefaultConnection",
$"Server={envs["DB_HOST"]};Port={envs["DB_PORT"]};Database={envs["DB_DATABASE"]};User Id={envs["DB_USER"]};Password={envs["DB_PASSWORD"]};"
))
);
}
In this example taken from the Web Sample, we are adding a required env, an optional env with a default value, and a custom mapping function that pulls multiple envs at once to build a connection string.
AddRequiredEnv
and AddOptionalEnv
both come with overloads that allow a conditional function to determine if the variable should be loaded into configuration.
This can be useful if you want to prevent clobbering other providers' values if they are already set.
You can chain as many functions as you want. If you are concerned about this cluttering up your Program.cs
, you can create a custom
extension method in your project that performs the configuration. See the Web Sample
for an example.
You can also create custom extension methods for IEnvConfigurationBuilder
that can encapsulate custom logic. For example,
if multiple projects need access to the same connection string, you could write an extension method that wraps the AddCustomMapper
function above
and share it in a common class library.
Product | Versions Compatible and additional computed target framework versions. |
---|---|
.NET | net5.0 was computed. net5.0-windows was computed. net6.0 was computed. 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 | netcoreapp2.0 was computed. netcoreapp2.1 was computed. netcoreapp2.2 was computed. netcoreapp3.0 was computed. netcoreapp3.1 was computed. |
.NET Standard | netstandard2.0 is compatible. netstandard2.1 was computed. |
.NET Framework | net461 was computed. net462 was computed. net463 was computed. net47 was computed. net471 was computed. net472 was computed. net48 was computed. net481 was computed. |
MonoAndroid | monoandroid was computed. |
MonoMac | monomac was computed. |
MonoTouch | monotouch was computed. |
Tizen | tizen40 was computed. 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.0
- DotNetEnv (>= 2.3.0)
- Microsoft.Extensions.Configuration (>= 6.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.