Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
560 views
in Technique[技术] by (71.8m points)

asp.net - How to read Elastic Beanstalk Environment Properties in .net?

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

Configuration > Software Configuration > Environment Properties

enter image description here

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.

Any ideas?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

In your .ebextensions/myoptions.config file:

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:

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

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

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

So you access this property using the standard AppSettings method:

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.

The Solution:

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

<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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...