EnvironmentVariables 0.2.0

dotnet add package EnvironmentVariables --version 0.2.0                
NuGet\Install-Package EnvironmentVariables -Version 0.2.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="EnvironmentVariables" Version="0.2.0" />                
For projects that support PackageReference, copy this XML node into the project file to reference the package.
paket add EnvironmentVariables --version 0.2.0                
#r "nuget: EnvironmentVariables, 0.2.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.
// Install EnvironmentVariables as a Cake Addin
#addin nuget:?package=EnvironmentVariables&version=0.2.0

// Install EnvironmentVariables as a Cake Tool
#tool nuget:?package=EnvironmentVariables&version=0.2.0                

EnvironmentVariables

NuGet version

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.)
  • Dictionary
  • Enum
  • Nullable<>

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

MIT

Product 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. 
.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. 
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.
  • .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.

Version Downloads Last updated
0.2.0 2,379 12/23/2020
0.1.0 493 8/25/2020