首页 > 解决方案 > Azure webjobs JobHostConfiguration 缺失,现在无法进行设置

问题描述

我想将我当前的 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();
}

我想让它使用正确的存储和仪表板连接字符串连续运行,而不使用配置文件。

标签: c#.netazureazure-webjobssdk

解决方案


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

1.在你的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.Set appsettings.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.输出: 在此处输入图像描述

有关更多详细信息,您可以参考本教程

更新

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

config.AddInMemoryCollection(settings);

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

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


推荐阅读