3
votes

I have made an Azure WebJob in c#. I have a web app on Azure, I added my WebJob to my subscription, all good works but in Application Settings I add a new entry, an example:

<add key="MyDesiredKey" value="1234" /> 

How can I get value of my key into my application when that runs on azure?

I try like this but not working, in this case no need to have that key in my web config no? When webjob run need to get value from Appsettings from my webapp stored on Azure

var keyFromAzureApp = ConfigurationManager.AppSettings["MyDesiredKey"];
3
Please add a comment when down-voting a question to specify how it can be improved. This is quite clear.Dead.Rabit

3 Answers

8
votes

How can I get value of my key into my application when that runs on azure?

According to my test if we have no setting on the azure portal, we will get the null value in the Webjob. Please add it on the azure portal, more detail please refer to the screenshot.

enter image description here

After doing that then all of following ways should work

 var myDesiredKey = ConfigurationManager.AppSettings["MyDesiredKey"];
 var environmentmyDesiredKey  = Environment.GetEnvironmentVariable("MyDesiredKey");
 var cloudmyDesiredKey = CloudConfigurationManager.GetSetting("MyDesiredKey");
3
votes

use CloudConfigurationManager instead as it tries in multiple places with a fallback mechanism.

var keyFromAzureApp = CloudConfigurationManager.GetSetting("MyDesiredKey");

Check out this to know when to use CloudConfigurationManager

2
votes

As bradbury9 explained you can use System.Configuration.ConfigurationManager.AppSettings["my-Key-Value"] for this.

It would be better if you add the application settings in the portal. These will be made available to all the processes on the instance as Environment variables at run time.

For app settings the name of the corresponding environment variable is prepended with APPSETTING_.

Here is a blog post which describes this: https://azure.microsoft.com/en-in/blog/windows-azure-web-sites-how-application-strings-and-connection-strings-work/