Using FunctionContext.RetryContext throws Nullreference on ServiceBusTrigger

Viewed 27

I try to receive the number of retries in a ServiceBusTrigger: (from a servicebusqueue)

using Microsoft.Azure.Functions.Worker;

[Function("SyncQueue")]
public async Task HandleSyncEvent([ServiceBusTrigger("sync-events", Connection = "ServiceBus-ConnectionString")] string syncEvent, FunctionContext context)
{
   var retryCount = context.RetryContext?.RetryCount ?? 0;
   if (retryCount > 0) {}
}

But while the RetryContext is not null the underlying Properties (RetryCount, MaxRetryCount) throw a NullreferenceException when I try to access them.

Can I solve this without wrapping a try..catch around the property when there are no retries?

Stacktrace:

System.NullReferenceException
  HResult=0x80004003
  Message=Object reference not set to an instance of an object.
  Source=Microsoft.Azure.Functions.Worker.Grpc
  StackTrace:
   at Microsoft.Azure.Functions.Worker.Grpc.GrpcRetryContext.get_RetryCount()
   at cse.Functions.HandleSyncQueue.<HandleSyncEvent>d__2.MoveNext() in C:\git\backend\services\src\Synchronization\cse.Functions\HandleSyncQueue.cs:line 26
1 Answers

I solved this by using DeliveryCount instead

Following the Docs this value should contain the exactly same value as context.RetryContext.RetryCount-1

so changing the Header to

[Function("SyncQueue")]
public async Task HandleSyncEvent([ServiceBusTrigger("sync-events", Connection = "ServiceBus-ConnectionString")] string syncEvent, int deliveryCount, FunctionContext context)
{
    if (deliveryCount > 1) {}
}

had the desired effect

Related