Azure 不从 WebJob 读取连接字符串

Azure not reading connection string from WebJob

我正在尝试使用我的 .NET Core 项目配置 Azure WebJob。每次我在 Azure 中执行作业时,它都会告诉我错误:

Make sure that you are setting a connection string named AzureWebJobsDashboard in your Microsoft Azure Website configuration by using the following format DefaultEndpointsProtocol=https;AccountName=NAME;AccountKey=KEY pointing to the Microsoft Azure Storage account where the Microsoft Azure WebJobs Runtime logs are stored.

这是我配置所有内容以及配置内容的代码:

    private static void ConfigureServices(IServiceCollection serviceCollection)
    {
        // Optional: Setup your configuration:
        var configuration = new ConfigurationBuilder()
            .SetBasePath(Directory.GetCurrentDirectory())
            .AddJsonFile("appsettings.json", optional: false, reloadOnChange: true)
            .AddEnvironmentVariables()
            .Build();

        // add any interfaces that will be needed here
        serviceCollection.AddScoped<IBatchJobService, BatchJobService>();

        // executes the job
        serviceCollection.AddScoped<ExecuteBatchJobs, ExecuteBatchJobs>();

        // One more thing - tell azure where your azure connection strings are
        var connStringDashboard = configuration["ConnectionStrings:AzureWebJobsDashboard"];
        var connStringStorage = configuration["ConnectionStrings:AzureWebJobsStorage"];

        Environment.SetEnvironmentVariable("AzureWebJobsDashboard", connStringDashboard);
        Environment.SetEnvironmentVariable("AzureWebJobsStorage", connStringStorage);
    }

这里是 appsettings.json 文件:

{
  "ConnectionStrings": {
    "AzureWebJobsDashboard": "DefaultEndpointsProtocol=https;AccountName=xxxxxxxxxxx;AccountKey=mykey;",
    "AzureWebJobsStorage": "DefaultEndpointsProtocol=https;AccountName=xxxxxxxxxxx;AccountKey=mykey;"
  }
}

我在 "var connStringDashboard = ...." 上的本地代码中设置了一个断点,它正确地读取了 appsettings.json 的值。

然后通过环境变量设置连接字符串。

关于我在设置连接字符串时出错的地方有什么想法吗?由于某种原因,Azure 似乎无法从环境变量中读取它们。

您需要在 Web App Application Settings 边栏选项卡的门户中设置 AzureWebJobsDashboard 连接字符串。 仪表板作为单独的站点扩展运行,无法访问 appsettings.json

将连接字符串添加到“应用程序设置”边栏选项卡上的连接字符串部分

您可以更改代码,以便将 appsettings.json 文件和 Azure 的 Application Settings 中的连接字符串重命名为其他内容(例如 WebJobsStorageWebJobsDashboard) ,然后就可以了。

Environment.SetEnvironmentVariable("AzureWebJobsStorage", configuration.GetConnectionString("WebJobsStorage"));
Environment.SetEnvironmentVariable("AzureWebJobsDashboard", configuration.GetConnectionString("WebJobsDashboard"));

更多细节,你可以参考这个issue