Search code examples
azureservicebusazure-webjobs

How to create service bus trigger webjob?


I check the doc use the below code to configure the webjob to create service bus trigger function.

    static void Main()
{
    var builder = new HostBuilder();
    builder.ConfigureWebJobs(b =>
    {
        b.AddAzureStorageCoreServices();
        b.AddServiceBus(sbOptions =>
        {
            sbOptions.MessageHandlerOptions.AutoComplete = true;
            sbOptions.MessageHandlerOptions.MaxConcurrentCalls = 16;
        });
    });
    var host = builder.Build();
    using (host)
    {

        host.Run();
    }
}

However when I try to implement it, the AddServiceBus method is not available, even add the service bus trigger function it always reports No job functions found error.

So where is the configuration error, thanks for any help.


Solution

  • Per my experience when you create the webjob you are not using the correct package. If you check the service bus binding doc, you will find it need Microsoft.Azure.WebJobs.Extensions.ServiceBus to provide Service Bus bindings.

    And under my test, the follow packages are what you need:

    1. Microsoft.Azure.WebJobs(>= 3.0.10)
    2. Microsoft.Azure.WebJobs.Extensions
    3. Microsoft.Azure.WebJobs.Extensions.ServiceBus
    4. Microsoft.Azure.WebJobs.ServiceBus

    With Microsoft.Azure.WebJobs.Extensions.ServiceBus, you will be able to use b.AddServiceBus() method and with Microsoft.Azure.WebJobs.ServiceBus to create ServiceBusTrigger function.

    The below is my test code, have a try.

    public static void Main(string[] args)
        {
            var builder = new HostBuilder();
            builder.ConfigureWebJobs(b =>
            {
                b.AddAzureStorageCoreServices();
                b.AddServiceBus();
            });
            builder.ConfigureLogging((context, b) =>
            {
                b.AddConsole();
            });
            var host = builder.Build();
            using (host)
            {
                host.Run();
            }
        }
    

    Function.cs

    public static void processservicebus(
        [ServiceBusTrigger("test", Connection = "ServiceBusConnection")]string myQueueItem,
        ILogger log)
        {
            log.LogInformation(myQueueItem);
        }
    

    enter image description here

    Hope this could help you, if you still have other problem please feel free to let me know.