Unable to set up batch processing in Azure service bus with azure function trigger

Bhavin Dave 0 Reputation points
2025-02-24T17:03:12.97+00:00

Hi,

I am trying to set up a service bus and have my azure function trigger whenever there is a message on queue. My single message processing is working fine. But I instead want to process a batch of messages. I specified the batch settings in host.json as well as tried adding a functions.json (was not available by default). Spend more than 20 hours but am still not able to make batch processing work. Documentation is extremely poor and is not helping either. I am getting close to my production deployment timeline. Appreciate your help and guidance.

Thanks

Bhavin

Azure Functions
Azure Functions
An Azure service that provides an event-driven serverless compute platform.
5,909 questions
{count} votes

1 answer

Sort by: Most helpful
  1. hossein jalilian 10,825 Reputation points Volunteer Moderator
    2025-02-24T17:18:01.59+00:00

    Hello Bhavin Dave,

    Thanks for posting your question in the Microsoft Q&A forum.

    Let's go through the steps to configure batch processing correctly:

    Update host.json: Ensure your host.json file includes the following configuration for Service Bus:

    {
      "version": "2.0",
      "extensions": {
        "serviceBus": {
          "prefetchCount": 100,
          "messageHandlerOptions": {
            "autoComplete": true,
            "maxConcurrentCalls": 16,
            "maxAutoRenewDuration": "00:05:00"
          },
          "batchOptions": {
            "maxMessageCount": 1000,
            "operationTimeout": "00:00:30"
          }
        }
      }
    }
    
    

    Update Function Code: Modify your Azure Function to handle batches of messages. Here's an example in C#:

    [FunctionName("ServiceBusBatchTrigger")]
    public static async Task Run(
        [ServiceBusTrigger("myqueue", Connection = "ServiceBusConnection")] ServiceBusReceivedMessage[] messages,
        ILogger log)
    {
        foreach (var message in messages)
        {
            log.LogInformation($"Processed message: {message.Body}");
        }
    }
    
    

    Batch Size: The actual number of messages in a batch may vary depending on the current load and message availability. Azure Functions will try to optimize based on your settings and the current state of the queue


    Please don't forget to close up the thread here by upvoting and accept it as an answer if it is helpful

    0 comments No comments

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.