gpt4 book ai didi

c# - 使用 .NET Core WebApp 访问 AWS ElasticBeanstalk 自定义环境变量

转载 作者:可可西里 更新时间:2023-11-01 09:07:58 25 4
gpt4 key购买 nike

我们在 Elastic Beanstalk 仪表板中设置了自定义环境变量,位于配置=>软件配置=>“环境属性”部分。在 C# MVC 5 项目中,我们可以通过使用 ConfigurationManager.AppSettings 查找这些变量来访问它们 - 这非常有效。

然而,在 .NET 核心中,我们不再使用 web.config。我们一直在尝试寻找一种访问环境变量的方法,但我们只找到了一个名为 AWSSDK.Extensions.NETCore.Setup 的 nuget 包。但是,这个包似乎没有让我们访问自定义变量。

如有任何帮助,我们将不胜感激。

最佳答案

根据我的研究和测试,这是 AWS Elastic Beanstalk for ASP.NET Core 1.1 应用程序的缺陷。今天刚遇到这个问题,解决它的唯一方法是使用 ASP.NET ConfigurationBuilder 加载 AWS 编写的配置(如果存在)并解析它。

AWS 最终应该会解决这个问题,在那之前您可以使用我正在使用的方法:

    public Startup(IHostingEnvironment env)
{
var builder = new ConfigurationBuilder()
.SetBasePath(env.ContentRootPath)
.AddJsonFile("appsettings.json", optional: true, reloadOnChange: true)
.AddJsonFile($"appsettings.{env.EnvironmentName}.json", optional: true)
.AddJsonFile(@"C:\Program Files\Amazon\ElasticBeanstalk\config\containerconfiguration", optional: true, reloadOnChange: true)
.AddEnvironmentVariables();

var config = builder.Build();

builder.AddInMemoryCollection(ParseEbConfig(config));

Configuration = builder.Build();
}

private static Dictionary<string, string> ParseEbConfig(IConfiguration config)
{
Dictionary<string, string> dict = new Dictionary<string, string>();

foreach (IConfigurationSection pair in config.GetSection("iis:env").GetChildren())
{
string[] keypair = pair.Value.Split(new[] { '=' }, 2);
dict.Add(keypair[0], keypair[1]);
}

return dict;
}

关于c# - 使用 .NET Core WebApp 访问 AWS ElasticBeanstalk 自定义环境变量,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44855453/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com