c# - Azure webjobs JobHostConfiguration 丢失,现在无法进行设置

标签 c# .net azure azure-webjobssdk

我想将当前的 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.在您的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.设置appsettings.json(注意设置其属性复制到输出目录始终复制):

{
  "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.输出: enter image description here

更多详情可以引用这个tutorial .

更新:

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

config.AddInMemoryCollection(settings);

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

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

关于c# - Azure webjobs JobHostConfiguration 丢失,现在无法进行设置,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55951336/

相关文章:

azure - 如何编写 azure 函数来触发 azure devops 管道?这可能吗?

c# - Google map - ASP.Net 中的简单方法?

c# - SSL通信的双向认证

c# - 无法在 .NET(完整)4.5 控制台应用程序中加载 Rosyln 生成的 PCL 程序集

c# - 如何在 WPF 应用程序中捕获命令行参数?

c# - 优化嵌套循环

azure - 使用队列订阅 Azure 服务总线主题

C# 将位图转换为索引颜色格式

c# - 无法通过计时器显示分钟

azure - 使用 networkconfig.xml 添加到 Azure 虚拟网络的第二个 ExpressRoute 连接