EnvGuard 0.1.0
.NET 8.0
This package targets .NET 8.0. The package is compatible with this framework or higher.
.NET Standard 2.0
This package targets .NET Standard 2.0. The package is compatible with this framework or higher.
dotnet add package EnvGuard --version 0.1.0
NuGet\Install-Package EnvGuard -Version 0.1.0
This command is intended to be used within the Package Manager Console in Visual Studio, as it uses the NuGet module's version of Install-Package.
<PackageReference Include="EnvGuard" Version="0.1.0" />
For projects that support PackageReference, copy this XML node into the project file to reference the package.
<PackageVersion Include="EnvGuard" Version="0.1.0" />
<PackageReference Include="EnvGuard" />
For projects that support Central Package Management (CPM), copy this XML node into the solution Directory.Packages.props file to version the package.
paket add EnvGuard --version 0.1.0
The NuGet Team does not provide support for this client. Please contact its maintainers for support.
#r "nuget: EnvGuard, 0.1.0"
#r directive can be used in F# Interactive and Polyglot Notebooks. Copy this into the interactive tool or source code of the script to reference the package.
#:package EnvGuard@0.1.0
#:package directive can be used in C# file-based apps starting in .NET 10 preview 4. Copy this into a .cs file before any lines of code to reference the package.
#addin nuget:?package=EnvGuard&version=0.1.0
#tool nuget:?package=EnvGuard&version=0.1.0
The NuGet Team does not provide support for this client. Please contact its maintainers for support.
EnvGuard
Strongly-typed, validated environment variables for .NET. Fail fast at startup, not in production.
The Problem
// Scattered across your codebase, crashes at runtime:
var connStr = Environment.GetEnvironmentVariable("DATABASE_URL"); // null in prod = 💥
var timeout = int.Parse(config["HttpClient:TimeoutSeconds"]!); // "thirty" = FormatException
var apiKey = config["OpenAI:ApiKey"]; // empty string = silent failure
You deploy, everything looks fine, then 10 minutes later a request hits a code path with a missing config value. Boom.
The Solution
using EnvGuard;
public class AppConfig
{
[EnvVar("DATABASE_URL", Required = true)]
public string DatabaseUrl { get; set; } = "";
[EnvVar("OPENAI_API_KEY", Required = true, Description = "OpenAI API key")]
public string OpenAiApiKey { get; set; } = "";
[EnvVar("HTTP_TIMEOUT_SECONDS", Default = "30")]
public int HttpTimeoutSeconds { get; set; }
[EnvVar("ENABLE_CACHE", Default = "true")]
public bool EnableCache { get; set; }
[EnvVar("LOG_LEVEL", Default = "Information")]
public LogLevel LogLevel { get; set; }
[EnvVar("ALLOWED_ORIGINS")] // Optional
public string? AllowedOrigins { get; set; }
}
Startup — Fail Fast
// Program.cs
var builder = WebApplication.CreateBuilder(args);
builder.Services.AddEnvGuard<AppConfig>(); // validates ALL vars at startup
If DATABASE_URL or OPENAI_API_KEY are missing, your app won't start:
EnvGuardException: 2 required environment variables are missing or invalid:
DATABASE_URL — Required, not set
OPENAI_API_KEY — Required, not set (OpenAI API key)
All errors at once. Not one at a time. Not at runtime.
Usage — Inject anywhere
app.MapGet("/health", (AppConfig config) => new {
timeout = config.HttpTimeoutSeconds, // int, not string
cache = config.EnableCache, // bool, not string
level = config.LogLevel // enum, not string
});
Installation
dotnet add package EnvGuard
Features
Type Coercion
| Type | Example Value | Notes |
|---|---|---|
string |
"hello" |
No conversion |
int |
"8080" |
int.Parse |
long |
"9999999999" |
long.Parse |
double |
"3.14" |
double.Parse |
bool |
"true", "1", "yes", "on" |
Case-insensitive, multiple formats |
enum |
"Warning" |
Case-insensitive Enum.Parse |
Uri |
"https://example.com" |
new Uri(...) |
TimeSpan |
"00:05:00" or "300" |
Parses format or total seconds |
Guid |
"a1b2c3..." |
Guid.Parse |
T? |
Any of above | Nullable wrapper — left null if not set |
Bool Parsing
Accepts: true/false, 1/0, yes/no, on/off (case-insensitive).
Defaults
[EnvVar("PORT", Default = "8080")]
public int Port { get; set; } // Uses 8080 if PORT is not set
Description in Errors
[EnvVar("OPENAI_API_KEY", Required = true, Description = "Get one at platform.openai.com")]
public string ApiKey { get; set; } = "";
// Error: OPENAI_API_KEY — Required, not set (Get one at platform.openai.com)
Check Without Throwing
var errors = EnvGuardValidator.Check<AppConfig>();
if (errors.Count > 0)
foreach (var err in errors)
Console.WriteLine($" ⚠ {err}");
Standalone Usage (No DI)
var config = EnvGuardValidator.Validate<AppConfig>();
Testing
var testEnv = new Dictionary<string, string>
{
["DATABASE_URL"] = "sqlite://test.db",
["API_KEY"] = "test-key"
};
var config = EnvGuardValidator.Validate<AppConfig>(
name => testEnv.GetValueOrDefault(name));
Why Not IOptions<T>?
| Feature | IOptions<T> |
EnvGuard |
|---|---|---|
| Missing values | Silent default(T) |
Throws at startup |
| All errors at once | No | Yes |
| Type coercion | Bind from config | int, bool, enum, Uri, TimeSpan, Guid |
| Bool formats | Only true/false |
true/1/yes/on |
| Testable | Needs IConfiguration mock |
Simple Func<string, string?> |
| Dependencies | Microsoft.Extensions.* |
Zero (netstandard2.0) |
License
MIT
| 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 is compatible. 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. net9.0 was computed. net9.0-android was computed. net9.0-browser was computed. net9.0-ios was computed. net9.0-maccatalyst was computed. net9.0-macos was computed. net9.0-tvos was computed. net9.0-windows was computed. net10.0 was computed. 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. |
| .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. |
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.
-
.NETStandard 2.0
- No dependencies.
-
net8.0
NuGet packages
This package is not used by any NuGet packages.
GitHub repositories
This package is not used by any popular GitHub repositories.
| Version | Downloads | Last Updated |
|---|---|---|
| 0.1.0 | 89 | 5/5/2026 |