Message Lock Duration
and Max Delivery Count
There is something called as Message Lock Duration
and Max Delivery Count
, so as per the config below it will be locked by the client for 5 minutes to be processed once the timeout happens it will cause an inner exception and will try to redeliver it incrementing the delivery count and up to the Max Delivery Count
as below 10.

Scan the log and If this is happening due to the Lock Duration Timeout
you can either try to manually renew it using BrokeredMessage.RenewLock Method in your code or automatically with OnMessageOptions.AutoRenewTimeout Property
Function Auto Scaling
You can also disable Auto Scaling
by making it to 1 which is by default 200, but this will have no/little effect to ServiceBus Triggerd Function
since even with a single instance it will make concurrent calls to service bus
to read message.

Singleton
Now for your answer you can try Singalton but I think even with singleton
if the function scales out it will be multiple singleton
as the function is not in a single box with a single instance. It might work in other triggers but not with service bus trigger
.
maxConcurrentCalls
in host.json
If your issue is not because of Message Lock Duration
it might be because of concurrent
calls to service bus which is by default 32. So you can add the following to your host.json
and minimize the values to 1 to test with outcome.
{
"version": "2.0",
"extensions": {
"serviceBus": {
"prefetchCount": 100,
"messageHandlerOptions": {
"autoComplete": true,
"maxConcurrentCalls": 32,
"maxAutoRenewDuration": "00:05:00"
},
"sessionHandlerOptions": {
"autoComplete": false,
"messageWaitTimeout": "00:00:30",
"maxAutoRenewDuration": "00:55:00",
"maxConcurrentSessions": 16
},
"batchOptions": {
"maxMessageCount": 1000,
"operationTimeout": "00:01:00",
"autoComplete": true
}
}
}
}
But if you just what to read the message is sequence from a ServiceBus Trigger Function
you just need to add the following in the host.json
to tell him stop making concurrent calls. And if you log the Sequence Number
of the message in the console you can verify it as it will now follow a sequence.
"extensions": {
"serviceBus": {
"messageHandlerOptions": {
"maxConcurrentCalls": 1
}
}
}
This will even avoid duplicates if you are doing upsert
as the records are created one by one in sequence but the downside it will be slow and compromise the performance.
maxConcurrentCalls
to 1. Refer stackoverflow.com/a/75898239/3057246 for more details.