AWS弹性beanstalk环境在asp.net core 1.0中 [英] AWS elastic beanstalk environment in asp.net core 1.0
问题描述
如何从弹性beanstalk中获取环境变量到asp.net核心mvc应用程序?我已经添加了一个包含app.config文件的.ebextensions文件夹,其中包含以下内容:
option_settings:
- option_name :HelloWorld
值:占位符
- option_name:ASPNETCORE_ENVIRONMENT
值:占位符
.ebextensions文件夹包含在发布包中。
部署时,这两个变量在aws elasticbeanstalk控制台中可见,配置>软件配置>环境变量
但是,当我尝试读取应用程序中的变量时,以下选项都不工作:
Environment.GetEnvironmentVariable(HelloWorld)//在控制器
配置[HelloWorld] //在startup.cs
有什么可以丢失的想法?谢谢。
有同样的问题,刚收到AWS支持有关此问题的回复。显然,环境变量在弹性beanstalk中没有适当地注入到ASP.NET Core应用程序中。
据我所知,他们正在努力解决问题。
解决方法是解析 C:\Program Files\Amazon\ElasticBeanstalk\config\containerconfiguration
进入配置构建器。这个文件是弹性beanstalk环境的一部分,应该在部署项目时可以访问。
首先添加文件:
$ {code> var builder = new ConfigurationBuilder()
.SetBasePath(C:\\Program Files\\Amazon\\\ElasticBeanstalk\\config )
.AddJsonFile(containerconfiguration,可选:true,reloadOnChange:true);
然后访问值:
var env = Configuration.GetSection(iis:env)。GetChildren();
pre>
foreach(envKeyValue in env)
{
var splitKeyValue = envKeyValue.Value.Split('=');
var envKey = splitKeyValue [0];
var envValue = splitKeyValue [1];
if(envKey ==HelloWorld)
{
// use envValue here
}
}
感谢
GP 从
Amazon Web ServicesHow 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
The .ebextensions folder is included in the publish package.
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.
解决方案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.
The workaround is to parse
C:\Program Files\Amazon\ElasticBeanstalk\config\containerconfiguration
into the configuration builder. This file is part of your elastic beanstalk environment and should be accessible upon deploying your project.First add the file:
var builder = new ConfigurationBuilder() .SetBasePath("C:\\Program Files\\Amazon\\ElasticBeanstalk\\config") .AddJsonFile("containerconfiguration", optional: true, reloadOnChange: true);
Then access the values:
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 } }
Courtesy of G.P. from Amazon Web Services
这篇关于AWS弹性beanstalk环境在asp.net core 1.0中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!