Azure webjobs JobHostConfiguration 丢失,无法立即进行设置

Azure webjobs JobHostConfiguration missing and can't figure out to do the settings now

我想将我当前的 webjobs 配置重写为 3.0 版,但我无法让它与文档一起使用,因为我不知道如何设置 dashboardconnectionstringstorageconnectionstring一个配置文件。

JobHostConfiguration config = new JobHostConfiguration
{
    NameResolver = new WebJobsNameResolver()
};

string defaultStorageConnectionString = string.Format( "Some dynamically generation string" );
config.DashboardConnectionString = defaultStorageConnectionString;
config.StorageConnectionString = defaultStorageConnectionString;

using(JobHost host = new JobHost(config))
{
    // The following code ensures that the WebJob will be running continuously
    host.RunAndBlock();
}

我想在不使用配置文件的情况下,使用正确的存储和仪表板连接字符串使其持续 运行。

3.0.0 NuGet 包更新(非测试版)带来了重大变化。它基于类似于 asp.net 主机的通用主机。您可以参考以下步骤:

1.Add这行代码在你的program.cs.

.ConfigureAppConfiguration((context, config) => {
    config.AddJsonFile("appsettings.json", optional: false, reloadOnChange: true);
})

整个代码在Program.cs.

using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.Hosting;
using Microsoft.Extensions.Logging;

namespace WebJob1template
{
    class Program
    {
        static void Main()
        {

            var builder = new HostBuilder()
                .UseEnvironment("Development")
                .ConfigureAppConfiguration((context, config) => {
                    config.AddJsonFile("appsettings.json", optional: false, reloadOnChange: true);
                })
                .ConfigureWebJobs(
                b =>
                {
                    b.AddAzureStorageCoreServices()
                    .AddAzureStorage()
                    .AddTimers()
                    .AddFiles();
                    //.AddDashboardLogging();
                })
                .ConfigureLogging((context, b) =>
                {
                    b.SetMinimumLevel(LogLevel.Debug);
                    b.AddConsole();
                })
                .UseConsoleLifetime();


            var host = builder.Build();

            using (host)
            {
                host.Run();
            }
        }
    }
}

2.Setappsettings.json(注意设置为属性Copy to Output DirectoryCopy always):

{
  "ConnectionStrings": {
    "AzureWebJobsDashboard": "xxxx",
    "AzureWebJobsStorage": "xxxx"
  }
}

3.Functions.cs:

using Microsoft.Azure.WebJobs;
using Microsoft.Extensions.Logging;

namespace WebJob1template
{
    public class Functions
    {        
        public static void ProcessQueueMessage([QueueTrigger("queue")] string message, ILogger log)
        {
            //log.WriteLine(message);
            log.LogInformation(message);
        }
    }
}

4.The 输出:

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

更新:

正如乔伊所说,我们可以使用

config.AddInMemoryCollection(settings);

public static Dictionary<string, string> settings = new Dictionary<string, string>
{
    {"ConnectionStrings:AzureWebJobsDashboard:0", "xxxxxxx"},
    {"ConnectionStrings:AzureWebJobsStorage:1", "xxxxxx"},
};

这样它就不会使用配置文件。这是有关如何使用 AddInMemoryCollection

的文章

根据 Joey Cai 的回答展开,有可能不使用 JSON 配置文件:

我们有:

var storageAcc = "<< your connection string >>"
var jobHostConfig = new JobHostConfiguration(storageAcc);
var host = new JobHost(jobHostConfig);
host.RunAndBlock();

现在:

var storageAcc = "<< your connection string >>"
var settings = new Dictionary<string, string>
{
    {"AzureWebJobsDashboard", storageAcc},
    {"AzureWebJobsStorage", storageAcc},
};

var builder = new HostBuilder();
builder.ConfigureAppConfiguration((context, config) =>
{
    config.AddInMemoryCollection(settings);
});

builder.ConfigureWebJobs(b =>
{
    b.AddAzureStorageCoreServices();
});

var host = builder.Build();

using (host)
{
    await host.RunAsync();
}

<讽刺>是的。多么漂亮的代码顺便说一句。

我的版本和 Joey 作为他的回答更新发布的版本之间的主要区别是我的字典必须包含关键字

"AzureWebJobsStorage"

而不是

"ConnectionStrings:AzureWebJobsStorage:0"

停止抱怨。这样我们就可以使用与以前相同的配置管理。