如何使用HostBuilder for WebJob? [英] How to use HostBuilder for WebJob?

查看:50
本文介绍了如何使用HostBuilder for WebJob?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

.Net 2.0引入了真正有用的控制台应用程序HostBuilder,就像我们使用的WebHostBuilder for Web Application一样.

With .Net 2.0 was introduced the really usefull HostBuilder for Console App like we have with WebHostBuilder for Web Application.

我现在关心的是如何使用带有QueueTrigger的WebJob来实现HostBuilder?

My concern is now how to implement the HostBuilder with WebJob with a QueueTrigger?

直到现在,我仍在使用JobActivator:

Until now, I was using JobActivator:

        var startup = new Startup();
        var serviceProvider = startup.ConfigureServices(new ServiceCollection());
        startup.Configure(serviceProvider);

        var jobHostConfiguration = new JobHostConfiguration()
        {
            JobActivator = new JobActivator(serviceProvider),
        };

        var host = new JobHost(jobHostConfiguration);
        host.RunAndBlock();

有关完整示例,这是我的代码: https://github.com/ranouf/DotNetCore-CosmosDbTrigger-WebJob/tree/master/CosmosDbTriggerWebJob.App

For a full sample, here is my code: https://github.com/ranouf/DotNetCore-CosmosDbTrigger-WebJob/tree/master/CosmosDbTriggerWebJob.App

是否有人已经通过QueueTrigger将HostBuilder用于WebJob?有可能吗?

Is there someone who already use HostBuilder for a WebJob with a QueueTrigger? Is it possible?

谢谢

推荐答案

然后,我已经弄清楚了.首先,请确保已升级软件包,以使用相应WebJob软件包的最新v3版本.

Right then, I've figured this out. Firstly ensure that you've upgraded your packages to use the latest v3 versions of the appropriate WebJob packages.

我发现我需要以下物品:

I found that I needed the following:

  1. Microsoft.Azure.WebJobs
  2. Microsoft.Azure.WebJobs.Core
  3. Microsoft.Azure.WebJobs.Extensions.ServiceBus

然后,您可以在Console方法的Main方法中使用构建器,如下所示:

Then you can use the builder in your Main method for the Console project as follows:

 static async Task Main()
    {
        var builder = new HostBuilder();
        builder.ConfigureAppConfiguration(cb =>
        {
            cb.AddJsonFile("appsettings.json");
        });
        builder.ConfigureWebJobs(b =>
        {
            b.AddServiceBus();
        });
        await builder.RunConsoleAsync();
    }

请注意,我已启用语言功能7.1以支持异步Main方法.如果愿意,可以改用"Wait()".然后我将一个appsettings.json文件添加到我的项目中,如下所示:

Note that I've enabled the language feature 7.1 to support async Main methods. You can use 'Wait()' instead if you prefer. Then I added an appsettings.json file to my project as follows:

{
   "ConnectionStrings": {
      "AzureWebJobsServiceBus": "Endpoint=sb://..."
    }
}

(并确保始终将文件复制到输出文件夹)最后也是最重要的一点是,我修改了触发函数,以包含Connection的名称,如下所示:

( and ensured the file is copied always to the output folder) And finally and most importantly I modified the trigger function to include the name of the Connection as follows:

    public static void ProcessQueueMessage([ServiceBusTrigger("[Your Queue Name]", Connection = "AzureWebJobsServiceBus")] string message, TextWriter log)
    {
        log.WriteLine(message);
    }

即使Connection的名称是默认名称,我似乎仍然必须在我的函数属性中对其进行定义.我也尝试过价值"方法,但是我无法做到这一点.然后,我开始从服务总线接收消息.

Even though the name of the Connection is the default I still seemed to have to define it in my function attributes. I tried the 'Values' approach too but I couldn't make that work. I then started receiving messages from the service bus.

这篇关于如何使用HostBuilder for WebJob?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆