1

The Azure Service Bus Queue triggered function would perform a default policy for sending message into the dead letter queue if message is poisoned. However, is there anyway that we could manually send the message to dead letter queue? There are couple of times that we wouldn't want Azure Function to perform its default policy due to some kind of internal or business exception, we would like to bind additional information to our message and manually send it to our Azure Service Bus Deadletter Queue.

In a previous version where it's using BrokeredMessage object, there is a method called

BrokeredMessage msg;
msg.DeadLetter();

However, in the most recent Functino 2.X, where it's using the Message object, that doesn't have this method somehow...

Drex
  • 3,346
  • 9
  • 33
  • 58

2 Answers2

2

With Functions 2.0 you use the .NET Standard Service Bus client. Message no longer has operations such as dead-lettering. Instead, you need to add an additional parameter of MessageReceiver type to be able to use DeadletterAsync() method it provides. You can find an example of how to use message receiver in my post.

Sean Feldman
  • 23,443
  • 7
  • 55
  • 80
  • When I use this `DeadletterAsync()` method with input lockToken, I got an exception like **MessageReceiver 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 know this is generally because message moved into DLQ instead of current queue, but is there anyway to remove this exception or release the lock token? Thank you for your kind suggestion! – Drex Sep 23 '19 at 19:05
  • Azure Functions built in a way where it assumes the control over the message. Either a message successful and the SDK completes it or an exception is thrown and the SDK will retry. If it retries too many times, the message is automatically dead-lettered. By dead-lettering yourself you're taking over the process and Functions doesn't know that. You could set `autoComplete` to false, but then you'd be always responsible to complete the message when you don't need to dead-letter it. It's mentioned in the post I linked to. – Sean Feldman Sep 23 '19 at 21:13
  • 2
    Thank you very much for the information! Yes your post did mention it clearly. I am trying to understand **you'd be always responsible to complete the message when you don't need to dead-letter it**, in that case, meaning if there is any exception, I either manually retry it (`messageReceiver.Abadon`) or manually push it to DLQ(`messageReceiver.DeadLetterAsync`), correct? – Drex Sep 24 '19 at 00:31
0

From my understanding there is no straight forward way but the following steps could help:

  • Go to the queues overview in the Azure portal and enable dead-lettering on message expiration on your subscription/queue.
  • Set TimeToLive on your message to say few seconds. (I did while creating a test queue, in the creation it is simple)
  • Push the message to your subscription.
  • Within few seconds you will have all your messages in dead-letter queue. //(Provided no app is reading from the subscription during those seconds)

Finished

Tomer
  • 531
  • 7
  • 19