Handling Dead Letter Queue(DLQ) using Azure Functions

, January 31, 2019 1511 Views

Microsoft Azure Service Bus is a secure platform for transferring the messages across various platforms and applications. This is a reliable service for asynchronous data transfer using messages. Queues are one among the data structures used while transmitting the data across the cloud. Messages are stored in the queue until processed at the receiver’s end.

In this blog, I am discussing the concept of reading the messages from the “Dead Letter Queues”. Now, what is a “Dead Letter Queues”?? It is a queue where all the undelivered messages in the Active Queue, are moved to. What could be the reason for such undelivered messages? – This could be due to various reasons like exceeding MaxDeliveryCount, exceeding TimeToLive, errors while processing subscription rules, Dead-lettering in ForwardTo or SendVia scenarios like: Message passes through more than 4 queues or topics that are chained together, The destination queue or topic is disabled or deleted etc.

For more details please refer:

These Dead Letter Queue messages can be read and managed either using the Azure Functions or the Logic Apps. Basically, you connect to your Dead Letter Queue in exactly the same way as your normal queue, but you need to concatenate “/$DeadLetterQueue” to the queue name. For example: “myQueue/$DeadLetterQueue”

Below are the ways to read the messages from the DLQ using the Azure Functions:

1) Using Code

In the below example, we are reading the messages from the DLQ at a regular time interval and are resending the messages to the Active Queue.

The comments in the below code will help you to understand the entire process aptly.


public static async Task Run([TimerTrigger("0 */2 * * * *")]TimerInfo myTimer, TraceWriter log)
log.Info($"C# Timer trigger function executed at: {DateTime.Now}");
string queueName = "Name of the Active Queue";
//Create a MessageFactory object to access the service bus where the messages are transferred
MessagingFactory messageFactory = MessagingFactory.CreateFromConnectionString("Endpoint=Provide the end point url of the Service Bus; SharedAccessKeyName=NAME; SharedAccessKey=KEY");

/*Create a receiver object that will receive the messages from the DLQ with PeekLock as the receiver mode. So that, the message will be locked till we resend the message to the Active Queue and execute the ‘Complete’ action of the BrokeredMessage object.*/
MessageReceiver deadletterReceiver = await         messageFactory.CreateMessageReceiverAsync(QueueClient.FormatDeadLetterPath(queueName), ReceiveMode.PeekLock);

//Create a sender object that will send the messages received from the DLQ to the Active queue
MessageSender sender = await messageFactory.CreateMessageSenderAsync(queueName);

//Read the messages received by the receiver
BrokeredMessage deadLetter = await deadletterReceiver.ReceiveAsync(TimeSpan.Zero);
if (deadLetter != null)
log.Info($"reason: " + deadLetter.Properties["DeadLetterReason"]);

/*Create a new message with the same content as read above from the body of the DLQ message*/
BrokeredMessage newMessage = new BrokeredMessage(deadLetter.GetBody<Stream>())
ContentType = deadLetter.ContentType,
CorrelationId = deadLetter.CorrelationId

//Send the message to the Active Queue
await sender.SendAsync(newMessage);
await deadLetter.CompleteAsync(); //Unlock the message and remove it from the DLQ
catch (Exception ex)
log.Info($"Exception: {ex}");

2) Using Trigger:

The other way of reading the Dead Letter Queue messages is by creating the Azure Function Trigger in the Azure portal. In the Function, provide the name of the DLQ as “QueueName/$DeadLetterQueue” as shown in the below image.

Note: If you want to access the undelivered message from the Topic then, the syntax of reading the Dead Letter Queue will be “TopicName/Subscriptions/SubscriptionName/$DeadLetterQueue”.

Now, define what should be done with the DLQ messages. Here, as shown in the below screenshot, we are sending the DLQ messages of “myqueue” to the Topic named of “queue” using the Azure Service Bus.

Sample code:

In this manner, we can handle the DLQ messages as required very easily using the Azure Functions.

We can also manage the Dead Letter Queue using the Logic App.

For more details, please refer below link:

I hope this helps you.


Insert data into Many-to-Many relationship in Dynamics CRM very easily & quickly, using the Drag and drop listbox.

Create a personal / system view in Dynamics CRM with all the fields on the form/s which you select for a particular entity using the View Creator.



INKEY is your solution partner.
Our focus is to deliver you in-time intelligent innovative solutions ("key") for the problems in hand. Maintaining a quality standard right from the inception of a project is our top most priority.

Our team of talented professionals will execute your projects with dedication and excellence. We take ownership and accountability for the effort that goes into meeting our client’s needs.

Years of experience and proven success of delivering innovative custom solutions.

More posts by

One response to “Handling Dead Letter Queue(DLQ) using Azure Functions”

  1. Arun Verma says:

    Hi Team

    We had a similar need and may provide some inputs to process dead letter messages into Active Q using Azure Functions/Triggers. Could you please provide some more details on the overall process.

Leave a Reply

Your email address will not be published. Required fields are marked *