SyntaxCircus.DotEnv
0.1.1
See the version list below for details.
dotnet add package SyntaxCircus.DotEnv --version 0.1.1
NuGet\Install-Package SyntaxCircus.DotEnv -Version 0.1.1
<PackageReference Include="SyntaxCircus.DotEnv" Version="0.1.1" />
<PackageVersion Include="SyntaxCircus.DotEnv" Version="0.1.1" />
<PackageReference Include="SyntaxCircus.DotEnv" />
paket add SyntaxCircus.DotEnv --version 0.1.1
#r "nuget: SyntaxCircus.DotEnv, 0.1.1"
#:package SyntaxCircus.DotEnv@0.1.1
#addin nuget:?package=SyntaxCircus.DotEnv&version=0.1.1
#tool nuget:?package=SyntaxCircus.DotEnv&version=0.1.1
SyntaxCircus.DotEnv
Reusable local dotenv configuration for ASP.NET Core hosts — and a second entry point for hosts that don't build an IConfigurationBuilder at all.
No support guaranteed. Published as-is and maintained on a best-effort basis. Issues and PRs are welcome, but there's no SLA — fork it or vendor what you need if that's not enough.
ASP.NET Core hosts
var builder = WebApplication.CreateBuilder(args);
builder.Configuration.AddUserSecrets<Program>(optional: true);
if (builder.Configuration.ShouldLoadDotEnv(builder.Environment))
{
builder.Configuration.AddSyntaxCircusDotEnvFiles(builder.Environment.ContentRootPath);
builder.Configuration.AddEnvironmentVariables();
}
builder.Configuration.AddUserSecrets<Program>(optional: true);
The default is enabled in Development and disabled elsewhere. Set DotEnv:Enabled to true or false in normal host configuration to override that default.
When enabled, .env is loaded before .env.local; .env.local overrides it. Both are below process/container environment variables and user secrets. Keys using __ map to the normal : configuration path separator.
Monorepo hosts sharing one .env file
If several hosts in the same repo (an API, a Web app, a Worker, ...) share one .env file, pass a hostPrefix so each host only picks up its own overrides plus whatever isn't prefixed at all:
builder.Configuration.AddSyntaxCircusDotEnvFiles(
builder.Environment.ContentRootPath,
hostPrefix: "MyAppApi__",
knownHostPrefixes: ["MyAppApi__", "MyAppWeb__", "MyAppWorker__"]);
A key like MyAppApi__ConnectionStrings__Default=... only applies when hostPrefix is "MyAppApi__" (with the prefix stripped before the __ → : mapping); a key like Logging__LogLevel__Default=Information with no matching prefix applies everywhere.
Non-ASP.NET-Core hosts (console apps, workers)
For hosts that set up their own environment before anything reads IConfiguration — or that don't use Microsoft.Extensions.Configuration at all — call DotEnvProcessLoader early in Main, before anything reads environment variables:
DotEnvProcessLoader.LoadFromRepositoryRoot(AppContext.BaseDirectory);
This walks up to the nearest .git directory, then sets real process environment variables from .env and .env.local (never overwriting a variable that was already set before either file loaded; .env.local overrides .env).
Contributing
Issues and pull requests are welcome:
- Keep changes focused, with a clear description of the behavior change.
- Match the existing code style (see
.editorconfig). - Call out any breaking changes to the public API in your PR description.
License
MIT — see LICENSE.txt.
| Product | Versions Compatible and additional computed target framework versions. |
|---|---|
| .NET | net10.0 is compatible. net10.0-android was computed. net10.0-browser was computed. net10.0-ios was computed. net10.0-maccatalyst was computed. net10.0-macos was computed. net10.0-tvos was computed. net10.0-windows was computed. |
-
net10.0
- DotNetEnv (>= 3.1.1)
NuGet packages
This package is not used by any NuGet packages.
GitHub repositories
This package is not used by any popular GitHub repositories.