ASP.NET Core 1.0 中的 AWS Elastic Beanstalk 环境变量 [英] AWS Elastic Beanstalk environment variables in ASP.NET Core 1.0
问题描述
如何将环境变量从 elastic beanstalk 获取到 asp.net core mvc 应用程序中?我添加了一个 .ebextensions 文件夹,其中包含 app.config 文件,内容如下:
How do I get environment variables from elastic beanstalk into an asp.net core mvc application? I have added a .ebextensions folder with app.config file in it with the following:
option_settings:
- option_name: HelloWorld
value: placeholder
- option_name: ASPNETCORE_ENVIRONMENT
value: placeholder
.ebextensions 文件夹包含在发布包中.
The .ebextensions folder is included in the publish package.
在部署时,这两个变量都在 aws elasticbeanstalk 控制台的配置 > 软件配置 > 环境变量中可见
On deployment, both the variables are visible in the aws elasticbeanstalk console at Configuration > Software Configuration > Environment Variables
但是,当我尝试读取应用程序中的变量时,以下选项均无效:
However, when I try to read the variables in the application, none of the below options are working:
Environment.GetEnvironmentVariable("HelloWorld") // In controller
Configuration["HelloWorld"] // In startup.cs
关于我可能遗漏的任何想法?谢谢.
Any ideas on what I could be missing? Thanks.
推荐答案
遇到了同样的问题,刚刚收到 AWS 支持人员关于此问题的回复.显然环境变量没有正确注入到弹性 beantalk 中的 ASP.NET Core 应用程序中.
Had the same problem, and just received a reply from AWS support about this issue. Apparently environment variables are not properly injected into ASP.NET Core applications in elastic beanstalk.
据我所知,他们正在努力解决这个问题.
As far as I know, they're working to fix the problem.
解决方法是将 C:Program FilesAmazonElasticBeanstalkconfigcontainerconfiguration
解析到配置生成器中.此文件是您的弹性 beanstalk 环境的一部分,在部署您的项目时应该可以访问.
The workaround is to parse C:Program FilesAmazonElasticBeanstalkconfigcontainerconfiguration
into the configuration builder. This file is part of your elastic beanstalk environment and should be accessible upon deploying your project.
首先添加文件:
var builder = new ConfigurationBuilder()
.SetBasePath("C:\Program Files\Amazon\ElasticBeanstalk\config")
.AddJsonFile("containerconfiguration", optional: true, reloadOnChange: true);
然后访问值:
var env = Configuration.GetSection("iis:env").GetChildren();
foreach (var envKeyValue in env)
{
var splitKeyValue = envKeyValue.Value.Split('=');
var envKey = splitKeyValue[0];
var envValue = splitKeyValue[1];
if (envKey == "HelloWorld")
{
// use envValue here
}
}
由G.P. 来自亚马逊网络服务
Courtesy of G.P. from Amazon Web Services
这篇关于ASP.NET Core 1.0 中的 AWS Elastic Beanstalk 环境变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!