Azure Function local debugging - how to restrict it to single thread or message?

Viewed 1315

I have an Azure Service Bus triggered Azure Function. When I run the Azure Function locally, it starts 16 threads and picks up 16 messages in each thread. How can I configure it so that it only runs one message so that I can debug it without the same breakpoint getting hit 16 times?

I tried to set configuration in host.json file (as below) to only pick up 1 message at a time from Azure Service Bus, but this didn't help.

{
  "version": "2.0",
  "extensions": {
    "serviceBus": {
      "prefetchCount": 100,
      "messageHandlerOptions": {
        "autoComplete": false,
        "maxConcurrentCalls": 1,
        "maxAutoRenewDuration": "00:55:00"
      }
    }
  }
}

Edit 1: What I currently do is triggering the function's admin endpoint via an http request that contains message input in body. The problem with this is that the http request body has to contain {"input":"{}"} and I have to spend time creating valid json every time with escaped double quotes. Would be much easier if I was able to configure the function to run single message at at time from service bus topic.

3 Answers

For queue-triggered Functions, host.json should be:

{
  "version": "2.0",
  "extensions": {
    "queues": {
      "batchSize": 1
    }
  }
}

Turned out I was making a mistake in how I ran the function (feeling ashamed..). Host.json file was not copied to the bin folder and therefore not recognized. The host.json configuration does work as expected and restrict the function to process one message at a time.

try setting this in your local.settings.json to override your host.json file. This way your don't need to change your host.json which prevents you from accidentally commit changes to host.json.

{
   "Values": { 
   "AzureFunctionsJobHost__extensions__serviceBus__messageHandlerOptions__maxConcurrentCalls": 1
   }
}

the result is that your host.json (at runtime) will be changed into this

"extensions": {
  "serviceBus": {
   "messageHandlerOptions": {
    ...
    "maxConcurrentCalls": 1 -> added setting
    ...
  }
}

}

Related