Extension library to provide some Environment functionality, for example for using with Docker-apps.
This appender is available as a NuGet package.
Run Install-Package Net.Bluewalk.DotNetEnvironmentExtensions
in the Package Manager Console or search for "DotNetEnvironmentExtensions" in your IDE's package management plug-in.
The following functionalities are available:
Method | Parameters | Result | Extends |
---|---|---|---|
GetEnvironmentVariable<T> |
name , optional defaultValue |
T |
|
GetEnvironmentVariable |
type , name , optional defaultValue |
object |
|
FromEnvironment |
autoCreateInstances |
void |
object |
FromEnvironment |
autoCreateInstances |
void |
Type |
Use the following attribute to decorate properties with environment variables: EnvironmentVariable
var useThis = EnvironmentExtensions.GetEnvironmentVariable<bool>("USE_THIS", false);
OR
var useThis = EnvironmentExtensions.GetEnvironmentVariable(typeof(bool), "USE_THIS", false);
Parameter autoCreateInstances
can be used to define if properties that are non-simple types need to me instantiated automatically.
Note: these classes need to have a parameter-less constructor
Example class
public class MySettings
{
[EnvironmentVariable(Name = "SOME_STRING", Default = "ABCdef")]
public string SomeString { get; set; }
[EnvironmentVariable(Name = "ENABLE_SMTHNG", Default = true)]
public bool EnableSomething { get; set; }
[EnvironmentVariable(Name = "MY_INT_VAL", Default = 1234)]
public int MyIntValue { get; set; }
public SubSettings SubSettings { get; set; }
}
public class SubSettings
{
[EnvironmentVariable(Name = "ANOTHER_STRING")]
public string AnotherString { get; set; }
[EnvironmentVariable(Name = "ANOTHER_INT_VAL", Default = -1)]
public int AnotherIntValue { get; set; }
}
Read from environment
var mySettings = typeof(MySettings).FromEnvironment();
OR
var mySettings = new MySettings();
mySettings.FromEnvironment();