Created
December 19, 2024 18:42
-
-
Save buchizo/3a3ee38d839ce5596834ad5ead986865 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{ | |
"FromJsonValue": "from json!" | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using ConsoleAppFramework; | |
using Microsoft.Extensions.Configuration; | |
using Microsoft.Extensions.DependencyInjection; | |
using Microsoft.Extensions.Options; | |
var app = ConsoleApp.Create() | |
.ConfigureDefaultConfiguration(config => | |
{ | |
config.AddEnvironmentVariables(); | |
}) | |
.ConfigureServices((configuration, service) => | |
{ | |
service.Configure<HogeOptions>(configuration); | |
}); | |
app.Add("", ([FromServices] IOptions<HogeOptions> hoge) => | |
{ | |
var a = hoge.Value.FromJsonValue; // ok | |
var b = Environment.GetEnvironmentVariable("FromJsonValue"); // NG (null) | |
}); | |
app.Run(args); | |
public class HogeOptions | |
{ | |
public string FromJsonValue { get; set; } = string.Empty; | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using ConsoleAppFramework; | |
using Microsoft.Extensions.Configuration; | |
using Microsoft.Extensions.DependencyInjection; | |
using Microsoft.Extensions.Options; | |
var app = ConsoleApp.Create() | |
.ConfigureDefaultConfiguration(config => | |
{ | |
config.AddEnvironmentVariables(); | |
}) | |
.ConfigureServices((configuration, service) => | |
{ | |
foreach(var v in configuration.AsEnumerable()) | |
{ | |
Environment.SetEnvironmentVariable(v.Key, v.Value); | |
} | |
service.Configure<HogeOptions>(configuration); | |
}); | |
app.Add("", ([FromServices] IOptions<HogeOptions> hoge) => | |
{ | |
var a = hoge.Value.FromJsonValue; // ok | |
var b = Environment.GetEnvironmentVariable("FromJsonValue"); // ok | |
}); | |
app.Run(args); | |
public class HogeOptions | |
{ | |
public string FromJsonValue { get; set; } = string.Empty; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment