Class EnvironmentVariableBinder
Class with utilities methods about environment variables.
Inheritance
System.Object
EnvironmentVariableBinder
Inherited Members
System.Object.Equals(System.Object)
System.Object.Equals(System.Object, System.Object)
System.Object.GetHashCode()
System.Object.GetType()
System.Object.MemberwiseClone()
System.Object.ToString()
System.Object.ReferenceEquals(System.Object, System.Object)
Namespace: StoneCo.Framework.EnvironmentVariable
Assembly: cs.temp.dll.dll
Syntax
public static class EnvironmentVariableBinder
Methods
BindEnvironmentVariable(String)
Open a file and read it looking for a tag to replace for a value storaged in a environment variable.
Declaration
public static string BindEnvironmentVariable(this string appSettingsDirectoryAndFolder)
Parameters
Type | Name | Description |
---|---|---|
System.String | appSettingsDirectoryAndFolder | folder and file with extension. |
Returns
Type | Description |
---|---|
System.String | value of appSettingsDirectoryAndFolder. |
Examples
For all examples below, let's following a list of environment variables:
Variable | Value |
---|---|
LOGS_ERROR_ENABLE | True |
LOGS_ERROR_HOST | https://domain-of-api-log.com/ |
ASPNETCORE_ENVIRONMENT | Development |
a) Into solution file appsettings/appsettings.json there are following content:
{
"Logs": {
"Error" : {
"Host": "#{LOGS_ERROR_HOST}#",
"Enable" : "#{LOGS_ERROR_ENABLE}#"
}
}
}
b) After command to transform:
using StoneCo.Framework.Host.Api.EnvironmentVariable;
configurationBuilder.AddJsonFile("appsettings/appsettings.json".BindEnvironmentVariable(), optional: true, reloadOnChange: false);
c) Settings file will has the following content:
{
"Logs": {
"Error" : {
"Host": "https://domain-of-api-log.com/",
"Enable" : "True"
}
}
}
Example 2:
a) Into solution file appsettings/appsettings.json there are following content:{
"Logs": {
"Error" : {
"Host": "#{LOGS_ERROR_HOST}#",
"Enable" : "#{LOGS_ERROR_ENABLE}#"
},
"Communication" : {
"Host": "#{LOGS_COMMUNICATION_HOST}#",
"Enable" : "#{LOGS_COMMUNICATION_ENABLE}#"
}
}
}
b) After command to transform:
using StoneCo.Framework.Host.Api.EnvironmentVariable;
configurationBuilder.AddJsonFile("appsettings/appsettings.json".BindEnvironmentVariable(), optional: true, reloadOnChange: false);
c) Settings file will has the following content:
{
"Logs": {
"Error" : {
"Host": "https://domain-of-api-log.com/",
"Enable" : "True"
},
"Communication" : {
"Host": "![NOT_FOUND]!",
"Enable" : "![NOT_FOUND]!"
}
}
}
Example 3:
a) Into solution, there is not file appsettings/appsettings.Development.json;
b) After command to transform:
using StoneCo.Framework.Host.Api.EnvironmentVariable;
configurationBuilder.AddJsonFile($"appsettings/appsettings.{hostingContext.HostingEnvironment.EnvironmentName}.json".BindEnvironmentVariable(), optional: true, reloadOnChange: false);
c) A file not found exception will be threw:
File appsettings/appsettings.Development.json not found.