[英]Avoid static value in ASP.NET Core Program.cs
In my CreateWebHostBuilder() method I've added the AWS Systems Manager Parameter Store as an additional source for Configuration Builder: 在我的CreateWebHostBuilder()方法中,我添加了AWS Systems Manager参数存储作为Configuration Builder的其他来源:
public class Program
{
public static void Main(string[] args)
{
CreateWebHostBuilder(args).Build().Run();
}
public static IWebHostBuilder CreateWebHostBuilder(string[] args)
{
return WebHost.CreateDefaultBuilder(args)
.ConfigureAppConfiguration(builder =>
{
builder.AddSystemsManager("/ConfigureStoreName/");
})
.UseStartup<Startup>();
}
}
Instead of hardcoding "/ConfigureStoreName/ " I'd like to make this a configuration value. 而不是硬编码“ / ConfigureStoreName / ”,我想将此配置值。
When I call .ConfigureAppConfiguration()
do I have access to the config values from appsettings.json that .CreateDefaultBuilder()
uses? 当我打电话
.ConfigureAppConfiguration()
我有从appsettings.json访问配置值.CreateDefaultBuilder()
使用? If so how would I update my code to call it? 如果是这样,我将如何更新代码来调用它? If not what is the best approach to avoid using a static value in the
CreateWebHostBuilder()
method? 如果不是,最好的方法是避免在
CreateWebHostBuilder()
方法中使用静态值?
Pre load setting file with that information. 带有该信息的预加载设置文件。
If for example the setting file contained 例如,如果包含设置文件
{
//...
"AWS": {
"Profile": "local-test-profile",
"Region": "us-west-2",
"ConfigureSource": {
"Path": "/ConfigureStoreName/"
}
}
//...
}
load that up in a configuration to extract the value. 将其加载到配置中以提取值。
public static IWebHostBuilder CreateWebHostBuilder(string[] args) {
var configuration = new ConfigurationBuilder()
.AddJsonFile("appsettings.json") //<-- or whichever file has that information
.Build();
string path = configuration.GetValue<string>("AWS:ConfigureSource:Path");
//Or a strongly typed model with aws options
return WebHost.CreateDefaultBuilder(args)
.ConfigureAppConfiguration(builder =>
{
builder.AddSystemsManager(path);
})
.UseStartup<Startup>();
}
The solution I ended up going with was to use an Environment Variable: 我最终使用的解决方案是使用环境变量:
public class Program
{
private static string _parameterStoreNamePath;
public static void Main(string[] args)
{
_parameterStoreNamePath = Environment.GetEnvironmentVariable("AWS_PARAMETER_STORE_NAME");
CreateWebHostBuilder(args).Build().Run();
}
public static IWebHostBuilder CreateWebHostBuilder(string[] args)
{
return WebHost.CreateDefaultBuilder(args)
.ConfigureAppConfiguration(builder =>
{
builder.AddSystemsManager(_parameterStoreNamePath);
})
.UseStartup<Startup>();
}
}
Notes: 笔记:
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.