Configuration.Extensions.EnvironmentFile 2.0.0

dotnet add package Configuration.Extensions.EnvironmentFile --version 2.0.0                
NuGet\Install-Package Configuration.Extensions.EnvironmentFile -Version 2.0.0                
This command is intended to be used within the Package Manager Console in Visual Studio, as it uses the NuGet module's version of Install-Package.
<PackageReference Include="Configuration.Extensions.EnvironmentFile" Version="2.0.0" />                
For projects that support PackageReference, copy this XML node into the project file to reference the package.
paket add Configuration.Extensions.EnvironmentFile --version 2.0.0                
#r "nuget: Configuration.Extensions.EnvironmentFile, 2.0.0"                
#r directive can be used in F# Interactive and Polyglot Notebooks. Copy this into the interactive tool or source code of the script to reference the package.
// Install Configuration.Extensions.EnvironmentFile as a Cake Addin
#addin nuget:?package=Configuration.Extensions.EnvironmentFile&version=2.0.0

// Install Configuration.Extensions.EnvironmentFile as a Cake Tool
#tool nuget:?package=Configuration.Extensions.EnvironmentFile&version=2.0.0                

NuGet Info GitHub License CI

Configuration.Extensions.EnvironmentFile

Unix style Environment files to configure .Net core applications

ConnectionStrings__Logs=User ID=root;Password=myPassword;Host=localhost;Port=5432;Database=myDataBase;

#Security section -- this line is omitted by the configuration provider
Security__Jwt__Key=q2bflxWAHB4fAHEU
Security__Jwt__ExpirationTime=00:05:00
Security__Jwt__Audience=https://always-use-https.com

Motivation

Having a development environment that resembles as much as possible to production is the best. In Unix servers, you can configure your background services with an environment file that has the format specified above, but what about in local? So many options but none matches that, so you can have them now, having a .env file (or more) copied with your files on build and loaded in the configuration.

How to use it

Install the package via Nuget

Install-Package Configuration.Extensions.EnvironmentFile

or .Net core command line

dotnet add package Configuration.Extensions.EnvironmentFile

Then configure your app like:

public static IHostBuilder CreateHostBuilder(string[] args) =>
    Host.CreateDefaultBuilder(args)
        .ConfigureAppConfiguration((hostingContext, config) =>
        {
            config
                .AddEnvironmentFile()
                .AddEnvironmentVariables(prefix: "MyCustomPrefix_");
        })
        .ConfigureWebHostDefaults(webBuilder =>
        {
            webBuilder.UseStartup<Startup>();
        });

Default behavior

  • The variables are loaded from a file called .env that is placed in the same directory as your applications.
  • Trimming is performed (usually spaces at the end are mistakes).
  • No quotes in values are trimmed (there is no need to add quotes, the library will handle = just fine).
public static IHostBuilder CreateHostBuilder(string[] args)
{
    Host
        .CreateDefaultBuilder(args)
        .ConfigureAppConfiguration((hostingContext, config) =>
        {
            config
                .AddEnvironmentFile(removeWrappingQuotes: true, trim: false)
                .AddEnvironmentVariables();
        })
        .ConfigureWebHostDefaults(webBuilder =>
    	{
            webBuilder.UseStartup<Startup>();
        });
}

Load multiple files

You can have several files also loaded, remember the last file will override the first one (if same variables are present)

public static IHostBuilder CreateHostBuilder(string[] args)
{
    Host
        .CreateDefaultBuilder(args)
        .ConfigureAppConfiguration((hostingContext, config) =>
        {
            config
                .AddEnvironmentFile() // Configuring from '.env' file
                .AddEnvironmentFile("database-config.env") // Overriding with 'database-config.env'
                .AddEnvironmentVariables();  // Overriding with environment variables
        })
        .ConfigureWebHostDefaults(webBuilder =>
    	{
            webBuilder.UseStartup<Startup>();
        });
}

Variable prefixes

You can specify variable prefixes to be omitted

public static IHostBuilder CreateHostBuilder(string[] args)
{
    Host
        .CreateDefaultBuilder(args)
        .ConfigureAppConfiguration((hostingContext, config) =>
        {
            config
                .AddEnvironmentFile() // Configuring from '.env' file
                .AddEnvironmentFile("with-prefix.env") // Variables like MyPrefix_MyVariable are loaded as MyPrefix_MyVariable
                .AddEnvironmentFile("with-prefix.env", prefix: "MyPrefix_") // Variables like MyPrefix_MyVariable are loaded as MyVariable
                .AddEnvironmentVariables();  // Overriding with environment variables
        })
        .ConfigureWebHostDefaults(webBuilder =>
    	{
            webBuilder.UseStartup<Startup>();
        });
}

Reload configuration on file change

Configuration can automatically update on file changes

public static IHostBuilder CreateHostBuilder(string[] args)
{
    Host
        .CreateDefaultBuilder(args)
        .ConfigureAppConfiguration((hostingContext, config) =>
        {
            config
                .AddEnvironmentFile() // Configuring from '.env' file
                .AddEnvironmentFile("reloadable.env", reloadOnChange: true) // This file will be watched for changes
                .AddEnvironmentVariables();  // Overriding with environment variables
        })
        .ConfigureWebHostDefaults(webBuilder =>
    	{
            webBuilder.UseStartup<Startup>();
        });
}

Logo Provided by Vecteezy

Product 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. 
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.

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
2.0.0 2,968 9/21/2024
1.3.0 24,591 4/28/2022
1.2.1 7,406 5/11/2021
1.2.0 372 5/10/2021
1.1.0 392 3/11/2021
1.0.0 412 3/9/2021
0.1.0 418 3/9/2021

Dropped support for Microsoft.Extensions.Configuration < 3.0.0