Dotenv.Extensions.Microsoft.DI
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. Refer to the API documentation.
Advantages
The advantages of using this wrapper are:
- No need to manually call the
EnvLoader.Loadmethod to set the environment variables from the .env file. - No need to manually call the
EnvBinder.Bindmethod to map the keys of the .env file with the model properties. - No need to manually register the service as a singleton:
IEnvReaderorAppSettings.
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
Loadmethod of theEnvLoaderclass to set the environment variables from a file named.env. - Invokes the
Bindmethod of theEnvBinderclass to map the keys of the .env file with theAppSettingsproperties. - Registers
AppSettingsas 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 HomeController : ControllerBase
{
private readonly AppSettings _settings;
public HomeController(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
LoadEnvmethod of theEnvLoaderclass 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
environmentis specified by the actual environment variableDOTNET_ENV.It should be noted that the default environment will be
developmentordevif the environment is never specified withDOTNET_ENV.Invokes the
Bindmethod of theEnvBinderclass to map the keys of the .env file with theAppSettingsproperties.Registers
AppSettingsas a singleton for the DI container.
Done, use AppSettings on the controllers and let the container perform the dependency injection.