Azure WebJobs 从哪里读取配置设置?

问题描述

我正在使用 Visual Studio 2019 和 Microsoft.NETCore.App v5.0.0 框架开发 Azure WebJob。我需要从配置中读取值,但我不明白我应该把它们放在哪里才能最终在 Azure 门户的 Azure WebJob 页面中覆盖它们。到目前为止,我已经创建了以下 appsettings.json

{
  "ConnectionStrings": {
    "AzureWebJobsStorage": "..."
  },"firstValue": "...","secondValue": "..."
}

以下公共方法

public static async Task Example(
   [BlobTrigger("%firstValue%/{blobName}")] Stream blobReceived
   [Blob("%secondValue%/{blobName}",FileAccess.Write)] Stream blobToWrite,ILogger logger)

firstValue 正确读取 secondValueappsettings.json 的值。也就是说,我看到其他项目使用 app.config 文件,然后使用以下内容检索其值:

ConfigurationManager.AppSettings["..."];
ConfigurationManager.ConnectionStrings["..."].ConnectionString;

我应该使用 app.config 而不是 appsettings.json 吗?当我们谈论 Azure WebJobs 时,我如何读取我放入 appsettings.json 文件中的值?

解决方法

您无法直接从 Azure WebJobs 访问应用设置,但可以考虑使用 Rest API 访问 Azure 门户中的 AppSettings。

实际上,WebJobs 是控制台应用程序,它使用系统中存储的环境变量,这与 Azure 门户中的 AppSettings 不同。存储在 IIS 中的 AppSettings 只能从此特定 Web 应用调用。

相关问答

错误1:Request method ‘DELETE‘ not supported 错误还原:...
错误1:启动docker镜像时报错:Error response from daemon:...
错误1:private field ‘xxx‘ is never assigned 按Alt...
报错如下,通过源不能下载,最后警告pip需升级版本 Requirem...