Dotenv.Extensions.Microsoft.DependencyInjection
1.0.1
See the version list below for details.
dotnet add package Dotenv.Extensions.Microsoft.DependencyInjection --version 1.0.1
NuGet\Install-Package Dotenv.Extensions.Microsoft.DependencyInjection -Version 1.0.1
<PackageReference Include="Dotenv.Extensions.Microsoft.DependencyInjection" Version="1.0.1" />
paket add Dotenv.Extensions.Microsoft.DependencyInjection --version 1.0.1
#r "nuget: Dotenv.Extensions.Microsoft.DependencyInjection, 1.0.1"
// Install Dotenv.Extensions.Microsoft.DependencyInjection as a Cake Addin #addin nuget:?package=Dotenv.Extensions.Microsoft.DependencyInjection&version=1.0.1 // Install Dotenv.Extensions.Microsoft.DependencyInjection as a Cake Tool #tool nuget:?package=Dotenv.Extensions.Microsoft.DependencyInjection&version=1.0.1
dotenv.core extensions for Microsoft.Extensions.DependencyInjection
This library adds extension methods for the Microsoft.Extensions.DependencyInjection package, which can be accessed through the IServiceCollection interface. So, this library is just a wrapper and behind the scenes what happens is that it uses the classes and methods from the dotenv.core project.
This project was created to add support for DI and can be used in ASP.NET Core projects.
Don't forget to visit the library website where you can find API documentation.
Advantages
The advantages of using this wrapper are:
- No need to manually call the
EnvLoader.Load
method to set the environment variables from the .env file. - No need to manually call the
EnvBinder.Bind
method to map the keys of the .env file with the model properties. - No need to manually register the service as a singleton:
IEnvReader
orAppSettings
.
Installation
If you're want to install the package from Visual Studio, you must open the project/solution in Visual Studio, and open the console using the Tools > NuGet Package Manager > Package Manager Console command and run the install command:
Install-Package Dotenv.Extensions.Microsoft.DependencyInjection
If you are making use of the dotnet CLI, then run the following in your terminal:
dotnet add package Dotenv.Extensions.Microsoft.DependencyInjection
Usage
You only need to invoke the AddDotEnv
method to add the environment vars using a service:
// Example in ASP.NET Core 6+
var builder = WebApplication.CreateBuilder(args);
// Add services to the container.
IServiceCollection services = builder.Services;
services.AddDotEnv<AppSettings>();
var app = builder.Build();
The following line of code:
services.AddDotEnv<AppSettings>();
It does several things:
- Invokes the
Load
method of theEnvLoader
class to set the environment variables from a file named.env
. - Invokes the
Bind
method of theEnvBinder
class to map the keys of the .env file with theAppSettings
properties. - Registers
AppSettings
as a singleton for the DI container.
Subsequently, the configuration class can be used in the controllers and the DI container will take care of injecting the instance when necessary:
public class ExampleController : ControllerBase
{
private readonly AppSettings _settings;
public ExampleController(AppSettings settings)
{
_settings = settings;
}
}
Note: You can also take a look at the source code of the example project.
Load .env file based on environment
Use the AddCustomEnv
method to adds the environment vars based on the environment (development, test, staging or production):
// Example in ASP.NET Core 6+
var builder = WebApplication.CreateBuilder(args);
// Add services to the container.
IServiceCollection services = builder.Services;
services.AddCustomEnv<AppSettings>();
var app = builder.Build();
The following line of code:
services.AddCustomEnv<AppSettings>();
It does several things:
Invokes the
LoadEnv
method of theEnvLoader
class to set the environment variables from a .env file.This method will search for these .env files in the following order:
.env.[environment].local
(has the highest priority).env.local
.env.[environment]
.env
(has the lowest priority)
The
environment
is specified by the actual environment variableDOTNET_ENV
.It should be noted that the default environment will be
development
ordev
if the environment is never specified withDOTNET_ENV
.Invokes the
Bind
method of theEnvBinder
class to map the keys of the .env file with theAppSettings
properties.Registers
AppSettings
as a singleton for the DI container.
Done, use AppSettings
on the controllers and let the container perform the dependency injection.
Contribution
Follow the steps below:
- Fork it
- Create your feature branch (git checkout -b my-new-feature)
- Commit your changes (git commit -am 'Added some feature')
- Push to the branch (git push origin my-new-feature)
- Create new Pull Request
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
- DotEnv.Core (>= 2.3.1)
- Microsoft.Extensions.DependencyInjection (>= 2.2.0)
NuGet packages
This package is not used by any NuGet packages.
GitHub repositories
This package is not used by any popular GitHub repositories.