Inspired by dotenv-safe
.
Using the EnvSafe
extension does the same thing that AddEnvironmentVariable()
but ensures that all necessary environment variables are defined after reading from Environment.GetEnvironmentVariables()
.
From the package manager console:
PM> Install-Package EnvSafe
# env.example (it has to be in the same directory as the files generated by the publish)
REDIS_HOST
REDIS_PORT
REDIS_INSTANCE
// launchSettings.json (or any launch settings file like `docker-compose` or `k8s yml`)
{
"profiles": {
"Development": {
"environmentVariables": {
"REDIS_HOST": "localhost",
"REDIS_PORT": "6379"
}
}
}
}
// Startup contructor or anywhere you need to build the app configuration
var builder = new ConfigurationBuilder()
.AddEnvironmentVariablesSafe(); // EnvSafe extension
Since the provided launchSettings.json
file does not contain all the variables defined in env.example
, an MissingEnvVarsException
is thrown:
The following variables were defined in env.example but are not present in the environment:
REDIS_INSTANCE
It's main purpose is to document the required environment variables the application needs to run correctly, failing fast in case of one of them missing and preventing unexpected behaviours in runtime.
Nowadays the only supported file name is env.example
, and it has to be in the content root path (next to the .dll
generated by the dotnet publish
).
EnvSafe
extension has the same overloads that IConfigurationBuilder.AddEnvironmentVariable()
.
- CI.
- Allow empty values.
- Support file by environment (
env.{Environment}.example
) with different required variables. - Allow any file name and directory.
- Use
env.example
as default value (using.AddEnvironmentVariableSafeOrDefault()
).