简体   繁体   English

如何从App Settings Azure(webapp)到我的webjob接收数据

[英]How to receive data from App Settings Azure (webapp) to my webjob

I have made an Azure WebJob in c#. 我已经用c#制作了Azure WebJob。 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: 我在Azure上有一个Web应用程序,我将WebJob添加到我的订阅中,一切正常,但是在“应用程序设置”中添加了一个新条目,例如:

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

How can I get value of my key into my application when that runs on azure? 在Azure上运行时,如何才能将密钥的值添加到我的应用程序中?

I try like this but not working, in this case no need to have that key in my web config no? 我尝试这样但不起作用,在这种情况下,不需要在我的Web配置中使用该密钥,是吗? When webjob run need to get value from Appsettings from my webapp stored on Azure 当webjob运行需要从存储在Azure上的webapp的Appsettings中获取价值时

var keyFromAzureApp = ConfigurationManager.AppSettings["MyDesiredKey"];

How can I get value of my key into my application when that runs on azure? 在Azure上运行时,如何才能将密钥的值添加到我的应用程序中?

According to my test if we have no setting on the azure portal, we will get the null value in the Webjob. 根据我的测试,如果我们在天蓝色门户上没有设置,我们将在Webjob中获得空值。 Please add it on the azure portal, more detail please refer to the screenshot. 请将其添加到azure门户中,更多详细信息请参阅屏幕截图。

在此处输入图片说明

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");

use CloudConfigurationManager instead as it tries in multiple places with a fallback mechanism. 请改用CloudConfigurationManager,因为它会使用后备机制在多个地方尝试。

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

Check out this to know when to use CloudConfigurationManager 查看此内容以了解何时使用CloudConfigurationManager

As bradbury9 explained you can use System.Configuration.ConfigurationManager.AppSettings ["my-Key-Value"] for this. 如bradbury9所述,您可以为此使用System.Configuration.ConfigurationManager.AppSettings [“ my-Key-Value”]。

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_ . 对于应用程序设置 ,相应环境变量的名称以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 : //azure.microsoft.com/zh-cn/blog/windows-azure-web-sites-how-application-strings-and-connection-strings-work/

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM