问题
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"];
回答1:
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.
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");
回答2:
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
回答3:
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/
来源:https://stackoverflow.com/questions/45144771/how-to-receive-data-from-app-settings-azure-webapp-to-my-webjob