如何阅读.net中的Elastic Beanstalk环境属性?

Nic*_*ick 12 .net asp.net amazon-web-services amazon-elastic-beanstalk

如何从我在此处找到的AWS Elastic Beanstalk应用程序中读取环境属性:

Configuration > Software Configuration > Environment Properties
Run Code Online (Sandbox Code Playgroud)

在此输入图像描述

以下方法均不起作用:

ConfigurationManager.AppSettings["MyServiceUrl"]
ConfigurationManager.AppSettings["aws:elasticbeanstalk:application:environment.MyServiceUrl"]
Environment.GetEnvironmentVariable("MyServiceUrl")
Environment.GetEnvironmentVariable("aws:elasticbeanstalk:application:environment.MyServiceUrl")
Run Code Online (Sandbox Code Playgroud)

"完全限定"名称尝试来自AWS EB文档.

有任何想法吗?

Mat*_*ser 17

在您的.ebextensions/myoptions.config文件中:

option_settings:
  - option_name: MyServiceUrl
    value: change me
Run Code Online (Sandbox Code Playgroud)

这将在您的EB环境属性部分中添加"MyServiceUrl"选项(正如您已经看到的那样).部署后,这会将以下内容添加到您的Web.Config文件中:

<appSettings>
  <add key="MyServiceUrl" value="change me" />
</appSettings>
Run Code Online (Sandbox Code Playgroud)

如果您将RDP导入EC2实例,您会看到这一点.

使用EB控制台更改属性时,将在Web.Config文件中修改设置.

因此,您使用标准AppSettings方法访问此属性:

string value = ConfigurationManager.AppSettings["MyServiceUrl"];
Run Code Online (Sandbox Code Playgroud)

抓住:

您需要确保您的Web.Config文件不包含此设置,否则EB不会替换它.如果您的Visual Studio部署包中包含此设置,则EB不会替换它,并且当您通过代码访问该属性时,您将始终收到已部署的值.

解决方案:

在您的Web.Release.config文件中,在Visual Studio部署期间删除该设置:

<appSettings>
  <add key="MyServiceUrl" xdt:Transform="Remove" xdt:Locator="Match(key)" />
</appSettings>
Run Code Online (Sandbox Code Playgroud)

这将Web.Config在Visual Studio部署期间删除该设置,并允许EB在EB部署期间将值添加到文件中.