EnvironmentVariables 0.2.0
dotnet add package EnvironmentVariables --version 0.2.0
NuGet\Install-Package EnvironmentVariables -Version 0.2.0
<PackageReference Include="EnvironmentVariables" Version="0.2.0" />
<PackageVersion Include="EnvironmentVariables" Version="0.2.0" />
<PackageReference Include="EnvironmentVariables" />
paket add EnvironmentVariables --version 0.2.0
#r "nuget: EnvironmentVariables, 0.2.0"
#:package EnvironmentVariables@0.2.0
#addin nuget:?package=EnvironmentVariables&version=0.2.0
#tool nuget:?package=EnvironmentVariables&version=0.2.0
EnvironmentVariables
Small .NET Standard library to easy access to all environment variables you need.
Targets .NET 5, .NET Standard 2.1, .NET Core 3.1
Installation
Install with NuGet Package Manager Console
Install-Package EnvironmentVariables
Install with .NET CLI
dotnet add package EnvironmentVariables
Usage
Step 1: Create class that describes all variables you need to access (POCO)
Use public properties, not fields. Add Env attribute with variable name to every property you want to get from environment.
using EnvironmentVariables;
public class EnvConfig
{
[Env("ASPNETCORE_ENVIRONMENT")]
public string AspNetCoreEnvironment { get; set; }
[Env("MY_ENV_INT")]
public int MyEnvInt { get; set; }
[Env("MY_ENV_BOOL")]
public bool MyEnvBool { get; set; }
[Env("MY_ENV_STRING_ARRAY")]
public string[] MyEnvStringArray { get; set; }
[Env("MY_ENV_INT_DICT")]
public Dictionary<int, string> MyEnvIntStrDictionary { get; set; }
}
Step 2: Create provider to access your variables
Create new EnvironmentProvider with class your crated earlier as a type parameter.
using EnvironmentVariables;
var provider = new EnvironmentProvider<EnvConfig>();
Access variables with Values property
Console.WriteLine(provider.Values.AspNetCoreEnvironment);
//Development
Console.WriteLine(provider.Values.MyEnvInt);
//123
Console.WriteLine(provider.Values.MyEnvBool);
//True
Dependency injection (.NET Core)
You can easily add provider to your services
services.AddSingleton<EnvironmentProvider<EnvConfig>>();
or POCO class
services.AddSingleton<EnvConfig>(
_ => new EnvironmentProvider<EnvConfig>().Values
);
Type support
This library supports:
string- all built-in value types (
bool,int,long,float,double,decimal, etc.) - collections (
IEnumerable,Array,List, etc.) DictionaryEnumNullable<>
You can check Tests project to make sure that particular type is supported.
Collections
For collections in your environment variables use , or ; as delimiter:
one,two,three
one;two;three
Dictionaries
For dictionaries in your environment variables use = or : as delimiter between key and value:
firstkey=123;secondkey=321;thirdkey=0
1:true, 2:false, 3:true
Converter ignores spaces so feel free to use it anywhere you want.
Other features
You may specify the function that will be used to access variables values
var provider = new EnvironmentProvider<EnvConfig>() {
EnvProvider = name => ThisIsMyFunctionToAccessTheValues(name)
};
Use selflog for debugging
var provider = new EnvironmentProvider<EnvConfig>() {
SelfLog = log => Console.WriteLine(log)
};
Use Reload to reload values from environment
provider.Reload();
License
| Product | Versions Compatible and additional computed target framework versions. |
|---|---|
| .NET | net5.0 is compatible. 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 was computed. 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 | netcoreapp3.0 was computed. netcoreapp3.1 is compatible. |
| .NET Standard | netstandard2.1 is compatible. |
| MonoAndroid | monoandroid was computed. |
| MonoMac | monomac was computed. |
| MonoTouch | monotouch was computed. |
| Tizen | tizen60 was computed. |
| Xamarin.iOS | xamarinios was computed. |
| Xamarin.Mac | xamarinmac was computed. |
| Xamarin.TVOS | xamarintvos was computed. |
| Xamarin.WatchOS | xamarinwatchos was computed. |
-
.NETCoreApp 3.1
- No dependencies.
-
.NETStandard 2.1
- No dependencies.
-
net5.0
- No dependencies.
NuGet packages
This package is not used by any NuGet packages.
GitHub repositories
This package is not used by any popular GitHub repositories.