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

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

我用 c# 创建了一个 Azure WebJob。我在 Azure 上有一个 Web 应用程序,我将我的 WebJob 添加到我的订阅中,一切正常,但在应用程序设置中我添加了一个新条目,例如:

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

当 运行 处于 Azure 状态时,我如何才能将我的密钥的值获取到我的应用程序中?

我这样尝试但没有用,在这种情况下不需要在我的网络配置中有那个密钥不是吗?当 webjob 运行 需要从我存储在 Azure 上的 webapp 的 Appsettings 中获取值时

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

改用 CloudConfigurationManager,因为它会在多个位置尝试使用回退机制。

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

查看 this 了解何时使用 CloudConfigurationManager

正如 bradbury9 所解释的那样,您可以为此使用 System.Configuration.ConfigurationManager.AppSettings["my-Key-Value"]。

如果在门户中添加应用程序设置会更好。这些将在 运行 时作为 环境变量 提供给实例上的所有进程。

对于app settings对应环境变量的名称前加APPSETTING_.

这里有一篇博客 post 描述了这个:https://azure.microsoft.com/en-in/blog/windows-azure-web-sites-how-application-strings-and-connection-strings-work/

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

根据我的测试,如果我们在 Azure 门户上没有设置,我们将在 Webjob 中获取空值。请在azure portal上添加,更多细节请参考截图。

完成后,以下所有方法都应该有效

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