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

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

问题描述

在 .Net 2.0 中引入了真正有用的 HostBuilder for Console App,就像我们在 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

是否有人已经将 HostBuilder 用于带有 QueueTrigger 的 Web 作业?可能吗?

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

然后,您可以在控制台项目的 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://..."
    }
}

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

( 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 用于 WebJob?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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