0

Using the latest .net framework: .net 7.0 I need to figure out how to dead letter a message if my azure function throws an exception.

MessageReceiver and ServiceBusMessageActions

I tried the following solutions:

How to manually send message to service bus dead letter queue in Azure Function?

How to redirect message to dead-letter queue Azure Service Bus

But the only input type my function supports is string. In both cases, my function would throw an error indicating it can't convert MessageReceiver to string or ServiceBusReceivedMessage to string.

QueueClient

Using IQueueClient to dead letter a message seems to be the microsoft recommended solution: https://learn.microsoft.com/en-us/dotnet/api/microsoft.servicebus.messaging.queueclient.deadletterasync?view=azure-dotnet

However I run into 2 issues:

  1. I don't know how to reference the azure service bus connection string from local.settings.json when newing up a QueueClient.
  2. If I hard code the connection string and queue name to get around the first issue, I have to generate a new Guid for the lock token. I then get this error: The lock supplied is invalid. Either the lock expired, or the message has already been removed from the queue, or was received by a different receiver instance. I'm not sure if I'm supposed to generate a new Guid for the token or where to get it? It wants a 32 digit GUID even though the token parameter is of type string not GUID. It will throw an error indicating it's not a GUID if I put a random string in there like "MyLockToken".

Here's my code:

    [Function("MyFunction")]        
    [ServiceBusOutput("%OutputQueueName%", Connection = "ServiceBusConnectionString")]
    public async Task<string> Run([ServiceBusTrigger("%InputQueueName%", Connection = "ServiceBusConnectionString")] string queueItem, string messageId) // , MessageReceiver messageReceiver
    {         
        try
        {
            // process message
        }
        catch(Exception ex)
        {
            IQueueClient queueClient = new QueueClient("ServiceBusConnectionString", "%InputQueueName%"); // error here, can't find settings in local.settings.json
            await queueClient.DeadLetterAsync("MyLockToken", "ProcessingError", ex.Message); // or Guid.NewGuid().ToString()
        }       
        
        return "message processed";
    }
goku_da_master
  • 4,257
  • 1
  • 41
  • 43
  • I edited the question to be more specific about using the QueueClient. The referenced duplicate question is not a duplicate because it's talking about using ServiceBusMessageActions and not QueueClient. – goku_da_master Aug 07 '23 at 18:16

0 Answers0