Environment Variables
Builder reference
| Method | Effect |
|---|---|
ApplyEncryptionKey(Func<string>) | Supplies the passphrase for encrypted: true registrations. See Encryption. |
Register(string name, bool encrypted = false) | Names an environment variable to resolve when the instance is built. |
ApplyValidatorFor(string name, Func<string, bool>, string reason) | Attaches a build-time validator to a registered name. See Validation. |
Build() | Resolves every registered name against the process environment and returns the live instance, or throws. |
Registering a variable
Register(name, encrypted = false) names an environment variable to resolve when the instance is built:
var envMgr = EnvVarConfigManager.BuildInstance("default")
.Register("API_BASE_URL")
.Register("API_KEY", encrypted: true)
.ApplyEncryptionKey(() => "at-least-twelve-characters")
.Build();Registering the same name twice on one instance throws InvalidOperationException. Registering encrypted: true has its own build-time requirement; see Encryption.
Read-once at build time
Every registered variable is resolved from Environment.GetEnvironmentVariable exactly once, during Build(), not lazily on first Get. If any registered variable is unset at that point, Build() throws a single ArgumentException that names every missing variable, not just the first one encountered:
try
{
EnvVarConfigManager.BuildInstance("default")
.Register("API_BASE_URL")
.Register("API_KEY")
.Build();
}
catch (ArgumentException ex)
{
// message lists every missing variable name registered on this instance
}Because resolution happens once at build time, Get has no refresh parameter: a process’s environment block doesn’t change after it starts, so there’s nothing to refresh. On Windows, that block is normally seeded at process start by merging the registry’s Machine and User stores (User overriding Machine on a name collision); a variable added to either afterward isn’t visible until the process restarts. Linux and macOS have no Machine/User equivalent, only the process block.
Reading
string apiKey = envMgr.Get("API_KEY");Get throws InvalidOperationException if the name was never registered on the instance. IsEncrypted(name) reports whether a given registration was configured as encrypted.
Next steps
- Encryption: encrypted registrations,
ConfigEncryptor - Validation:
ApplyValidatorForfor environment variables