dotenv.net 1.0.6

A library to read .env files and add then values to the environment variables of the application.

Install-Package dotenv.net -Version 1.0.6
dotnet add package dotenv.net --version 1.0.6
<PackageReference Include="dotenv.net" Version="1.0.6" />
For projects that support PackageReference, copy this XML node into the project file to reference the package.
paket add dotenv.net --version 1.0.6
The NuGet Team does not provide support for this client. Please contact its maintainers for support.

dotenv.net

CircleCI NuGet Badge License: MIT Coverage Status

dotenv.net is a zero-dependency module that loads environment variables from a .env environment variable file into System.Environment. It has built in support for the in-built dependency injection framework packaged with ASP.NET Core. It now comes packaged with an interface that allows for reading environment variables wihtout repeated calls to Environment.GetEnvironmentVariable(&quot;KEY&quot;);. If you have ideas or issues, create an issue.

Contributors

Big ups to those who have contributed to this library. :clap:

@bolorundurowb @joliveros @vizeke @merqlove @tracker1 @NaturalWill

Usage

Conventional

First install the library as a dependency in your application from nuget

Install-Package dotenv.net

or

dotnet add package dotenv.net

or for paket

paket add dotenv.net

Create a file with no filename and an extension of .env.

A sample .env file would look like this:

DB_HOST=localhost
DB_USER=root
DB_PASS=s1mpl3

in the Startup.cs file or as early as possible in your code add the following:

using dotenv.net;


...


DotEnv.Config();

the values saved in your .env file would be avaibale in your application and can be accessed via

Environment.GetEnvironmentVariable("DB_HOST"); // would output 'localhost'

Using with DI (IServiceCollection)

If using with ASP.NET Core or any other system that uses IServiceCollection for its dependency injection, in the Startup.cs file

public void ConfigureServices(IServiceCollection services)
{
    ...

    // configure dotenv
    services.AddEnv(builder => {
        builder
        .AddEnvFile("/custom/path/to/your/env/vars")
        .AddThrowOnError(false)
        .AddEncoding(Encoding.ASCII);
    });
}

With this, your application would have the env file variables imported.

Options

ThrowError

Default: true

You can specify if you want the library to error out if any issue arises or fail silently.

DotEnv.Config(false); //fails silently
Path

Default: .env

You can specify a custom path if your file containing environment variables is
named or located differently.

DotEnv.Config(true, "/custom/path/to/your/env/vars");
Encoding

Default: Encoding.UTF8

You may specify the encoding of your file containing environment variables
using this option.

DotEnv.Config(true, ".env", Encoding.Unicode);
Trim Values

Default: true

You may specify whether or not you want the values retrieved to be trimmed i.e have all leading and trailing whitepaces removed.

DotEnv.Config(true, ".env", Encoding.Unicode, false);

Support For IEnvReader

With v1.0.6 and above an interface IEnvReader has been introduced that specifies methods that help with reading values from the environment easily. The library has a default implementation EnvReader that can be added to the default ASP.NET Core DI framework (IServiceCollection).

Using EnvReader

using dotenv.net.Utilities;
...

var envReader = new EnvReader();
var value = envReader.GetValue("KEY");

Using IEnvReader with DI

In the StartUp.cs file, in the ConfigureServices method

...

public void ConfigureServices(IServiceCollection services)
{
    ...
    services.AddEnvReader();
    ...
}

In the rest of your application, the IEnvReader interface can get injected and used. For example, in a SampleController class for example:

public class SampleController
{
    private readonly IEnvReader _envReader;
    
    public SampleController(IEnvReader envReader)
    {
        _envReader = envReader;
    }
}

IEnvReader Methods

string GetValue(string key)

Default: null

Retrieve a value from the current environment by the given key and return null if a value does not exist for that key.

T GetValue<T>(string key)

Default: default(T)

A generic method that allows for a typed value to be retrieved from the environment variables and returns the default for the type. This functionality is limited to structs currently.

bool TryGetValue(string key, out string value)

Default: null

A safer method to use when retrieving values from the environment as it returns a boolean value stating whether it successfully retrieved the value required.

bool TryGetValue<T>(string key, out T value)

Default: default(T)

A safer method to use when retrieving values from the environment as it returns a boolean value stating whether it successfully retrieved and coverted the value required.

dotenv.net

CircleCI NuGet Badge License: MIT Coverage Status

dotenv.net is a zero-dependency module that loads environment variables from a .env environment variable file into System.Environment. It has built in support for the in-built dependency injection framework packaged with ASP.NET Core. It now comes packaged with an interface that allows for reading environment variables wihtout repeated calls to Environment.GetEnvironmentVariable(&quot;KEY&quot;);. If you have ideas or issues, create an issue.

Contributors

Big ups to those who have contributed to this library. :clap:

@bolorundurowb @joliveros @vizeke @merqlove @tracker1 @NaturalWill

Usage

Conventional

First install the library as a dependency in your application from nuget

Install-Package dotenv.net

or

dotnet add package dotenv.net

or for paket

paket add dotenv.net

Create a file with no filename and an extension of .env.

A sample .env file would look like this:

DB_HOST=localhost
DB_USER=root
DB_PASS=s1mpl3

in the Startup.cs file or as early as possible in your code add the following:

using dotenv.net;


...


DotEnv.Config();

the values saved in your .env file would be avaibale in your application and can be accessed via

Environment.GetEnvironmentVariable("DB_HOST"); // would output 'localhost'

Using with DI (IServiceCollection)

If using with ASP.NET Core or any other system that uses IServiceCollection for its dependency injection, in the Startup.cs file

public void ConfigureServices(IServiceCollection services)
{
    ...

    // configure dotenv
    services.AddEnv(builder => {
        builder
        .AddEnvFile("/custom/path/to/your/env/vars")
        .AddThrowOnError(false)
        .AddEncoding(Encoding.ASCII);
    });
}

With this, your application would have the env file variables imported.

Options

ThrowError

Default: true

You can specify if you want the library to error out if any issue arises or fail silently.

DotEnv.Config(false); //fails silently
Path

Default: .env

You can specify a custom path if your file containing environment variables is
named or located differently.

DotEnv.Config(true, "/custom/path/to/your/env/vars");
Encoding

Default: Encoding.UTF8

You may specify the encoding of your file containing environment variables
using this option.

DotEnv.Config(true, ".env", Encoding.Unicode);
Trim Values

Default: true

You may specify whether or not you want the values retrieved to be trimmed i.e have all leading and trailing whitepaces removed.

DotEnv.Config(true, ".env", Encoding.Unicode, false);

Support For IEnvReader

With v1.0.6 and above an interface IEnvReader has been introduced that specifies methods that help with reading values from the environment easily. The library has a default implementation EnvReader that can be added to the default ASP.NET Core DI framework (IServiceCollection).

Using EnvReader

using dotenv.net.Utilities;
...

var envReader = new EnvReader();
var value = envReader.GetValue("KEY");

Using IEnvReader with DI

In the StartUp.cs file, in the ConfigureServices method

...

public void ConfigureServices(IServiceCollection services)
{
    ...
    services.AddEnvReader();
    ...
}

In the rest of your application, the IEnvReader interface can get injected and used. For example, in a SampleController class for example:

public class SampleController
{
    private readonly IEnvReader _envReader;
    
    public SampleController(IEnvReader envReader)
    {
        _envReader = envReader;
    }
}

IEnvReader Methods

string GetValue(string key)

Default: null

Retrieve a value from the current environment by the given key and return null if a value does not exist for that key.

T GetValue<T>(string key)

Default: default(T)

A generic method that allows for a typed value to be retrieved from the environment variables and returns the default for the type. This functionality is limited to structs currently.

bool TryGetValue(string key, out string value)

Default: null

A safer method to use when retrieving values from the environment as it returns a boolean value stating whether it successfully retrieved the value required.

bool TryGetValue<T>(string key, out T value)

Default: default(T)

A safer method to use when retrieving values from the environment as it returns a boolean value stating whether it successfully retrieved and coverted the value required.

Release Notes

- Rework the app logic to make more maintainable
- Increase speed and reduce memory allocations by replacing string manipulation with IReadOnlySpan<T>.
- Add class for reading environment variables as strings and typed values
- Improve testing and upgrade CI config

This package is not used by any popular GitHub repositories.

Version History

Version Downloads Last updated
1.0.6 1,824 6/29/2019
1.0.5 103 6/27/2019
1.0.4 35,197 10/21/2018
1.0.3 16,696 2/17/2018
1.0.2 408 1/15/2018
1.0.1 383 12/31/2017
1.0.0 1,434 11/22/2017