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

时间:2020-12-06 23:19:21

How can I read the Environment Properties from my AWS Elastic Beanstalk Application found here:

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

Configuration > Software Configuration > Environment Properties

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

None of the following approaches work:

以下方法均无效:

ConfigurationManager.AppSettings["MyServiceUrl"]
ConfigurationManager.AppSettings["aws:elasticbeanstalk:application:environment.MyServiceUrl"]
Environment.GetEnvironmentVariable("MyServiceUrl")
Environment.GetEnvironmentVariable("aws:elasticbeanstalk:application:environment.MyServiceUrl")

The 'fully qualified' name attempt comes from the AWS EB documentation.

“完全限定”名称尝试来自AWS EB文档。

Any ideas?

有任何想法吗?

2 个解决方案

#1


14  

In your .ebextensions/myoptions.config file:

在.ebextensions / myoptions.config文件中:

option_settings:
  - option_name: MyServiceUrl
    value: change me

This will add the "MyServiceUrl" option in your EB Environment Properties section (as you're seeing already). When deployed, this will add the following to your Web.Config file:

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

<appSettings>
  <add key="MyServiceUrl" value="change me" />
</appSettings>

If you RDP into your EC2 instance, you'll see this.

如果你进入你的EC2实例,你会看到这个。

When you change the property using the EB console, the setting will be modified in your Web.Config file.

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

So you access this property using the standard AppSettings method:

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

string value = ConfigurationManager.AppSettings["MyServiceUrl"];

The Catch:

抓住:

You need to ensure that your Web.Config file does not contain this setting, otherwise EB does not replace it. If your Visual Studio deployment package includes this setting, then EB will not replace it and you will always receive the deployed value when you access the property via your code.

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

The Solution:

解决方案:

In you Web.Release.config file, have the setting removed during Visual Studio deployment:

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

<appSettings>
  <add key="MyServiceUrl" xdt:Transform="Remove" xdt:Locator="Match(key)" />
</appSettings>

This will remove the setting from Web.Config during Visual Studio deployment and will allow EB to add the value into the file during EB deployment.

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

#2


0  

It looks like this behavior has changed in Elastic Beanstalk. The docs now say

看起来这种行为在Elastic Beanstalk中已经发生了变化。文档现在说

Settings applied in the AWS Management Console override the same settings in configuration files, if they exist. This lets you have default settings in configuration files, and override them with environment specific settings in the console.

AWS管理控制台中应用的设置会覆盖配置文件中的相同设置(如果存在)。这使您可以在配置文件中使用默认设置,并使用控制台中的特定于环境的设置覆盖它们。

So you can now use the same config names in your web.config and in the Elastic Beanstalk config, and the Elastic Beanstalk values will override any in your web.config. It looks like EB simply adds new entries to the web.config file, so there will be two entries for any values defined in both places. Since the EB-added entries are later in the file they take precedence.

因此,您现在可以在web.config和Elastic Beanstalk配置中使用相同的配置名称,并且Elastic Beanstalk值将覆盖web.config中的任何值。看起来EB只是在web.config文件中添加了新条目,因此在两个地方定义的任何值都会有两个条目。由于EB添加的条目稍后在文件中,因此它们优先。

#1


14  

In your .ebextensions/myoptions.config file:

在.ebextensions / myoptions.config文件中:

option_settings:
  - option_name: MyServiceUrl
    value: change me

This will add the "MyServiceUrl" option in your EB Environment Properties section (as you're seeing already). When deployed, this will add the following to your Web.Config file:

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

<appSettings>
  <add key="MyServiceUrl" value="change me" />
</appSettings>

If you RDP into your EC2 instance, you'll see this.

如果你进入你的EC2实例,你会看到这个。

When you change the property using the EB console, the setting will be modified in your Web.Config file.

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

So you access this property using the standard AppSettings method:

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

string value = ConfigurationManager.AppSettings["MyServiceUrl"];

The Catch:

抓住:

You need to ensure that your Web.Config file does not contain this setting, otherwise EB does not replace it. If your Visual Studio deployment package includes this setting, then EB will not replace it and you will always receive the deployed value when you access the property via your code.

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

The Solution:

解决方案:

In you Web.Release.config file, have the setting removed during Visual Studio deployment:

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

<appSettings>
  <add key="MyServiceUrl" xdt:Transform="Remove" xdt:Locator="Match(key)" />
</appSettings>

This will remove the setting from Web.Config during Visual Studio deployment and will allow EB to add the value into the file during EB deployment.

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

#2


0  

It looks like this behavior has changed in Elastic Beanstalk. The docs now say

看起来这种行为在Elastic Beanstalk中已经发生了变化。文档现在说

Settings applied in the AWS Management Console override the same settings in configuration files, if they exist. This lets you have default settings in configuration files, and override them with environment specific settings in the console.

AWS管理控制台中应用的设置会覆盖配置文件中的相同设置(如果存在)。这使您可以在配置文件中使用默认设置,并使用控制台中的特定于环境的设置覆盖它们。

So you can now use the same config names in your web.config and in the Elastic Beanstalk config, and the Elastic Beanstalk values will override any in your web.config. It looks like EB simply adds new entries to the web.config file, so there will be two entries for any values defined in both places. Since the EB-added entries are later in the file they take precedence.

因此,您现在可以在web.config和Elastic Beanstalk配置中使用相同的配置名称,并且Elastic Beanstalk值将覆盖web.config中的任何值。看起来EB只是在web.config文件中添加了新条目,因此在两个地方定义的任何值都会有两个条目。由于EB添加的条目稍后在文件中,因此它们优先。