ASP.NET Core 1.0中的AWS Elastic Beanstalk环境变量 [英] AWS Elastic Beanstalk environment variables in ASP.NET Core 1.0

本文介绍了ASP.NET Core 1.0中的AWS Elastic Beanstalk环境变量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何从弹性beantalk中获取环境变量到asp.net核心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 Files\Amazon\ElasticBeanstalk\config\containerconfiguration解析到配置生成器中.该文件是您的Elastic beantalk环境的一部分,在部署项目时应该可以访问.

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.

首先添加文件:

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. 亚马逊网络服务

这篇关于ASP.NET Core 1.0中的AWS Elastic Beanstalk环境变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆