This article is contributed. See the original author and article here.

Azure Service Bus queues and topic subscriptions provide a secondary subqueue, called a dead-letter queue(DLQ). The dead-letter queue need not to be explicitly created and can’t be deleted or otherwise managed independent of the main entity.
Azure Service Bus messaging overview – Azure Service Bus | Microsoft Docs


Messages that can’t be processed because of various reasons fall into DLQ. Below are few conditions where messages will fall into DLQ:
1. Not matching with the filter condition
2. TTL expired, header exceed
3. Quota exceed for header size
4. Max delivery count reached
5. Session enabled and sending messages without sessionID
6. Using more than 4 forward to

Case:
To receive DLQ messages from queue/subscription


 


Pre-Requisites:


1. Service Bus namespace
2. Already created queue/subscription
3. Should have messages in DLQ either for queue/subscription
4. Service Bus Explorer


We have multiple ways to receive messages from DLQ.


Using Service Bus Explorer:



  1. Download the “Service Bus Explorer” from: https://github.com/paolosalvatori/ServiceBusExplorer

  2. Open service bus explorer and click File and connect it.


         ankitaja_0-1613840932569.png


 



  1. From the drop down, select connection string and provide the connection string of the namespace level.


        ankitaja_1-1613840932590.png



  1. Once it is successfully connected, you will see Service Bus Explorer shows the count of the DLQ message.
    In the below screenshot, there are 11 messages currently in the DLQ for the queue “ankitatest“.ankitaja_2-1613840932616.png

     



  2. To receive messages from DLQ through SB explorer, you need to click on that particular queue and then click on “Deadletter” tab then one dialogue box will pop up then you need to click on “Receive and Delete”. The default value is Top10 so top10 messages will be received from DLQ.


        The updated DLQ message count is now 1.


        ankitaja_3-1613840932639.png


 


Through C# Code:

In the given screenshot, we have 12 messages in DLQ for queue and we wanted to receive them.


ankitaja_4-1613840932642.png

I will run the below code which will receive the message from the mentioned queue. Once you run the code successfully, you will see the message ID in the console window as below.


ankitaja_5-1613840932643.png

Now, check on SB explorer and you will see 1 message has been gone from DLQ.


ankitaja_6-1613840932646.png


 


 


 


 

class Program
    {
        static void Main(string[] args)
        {
            RetrieveMessageFromDeadLetterForQueue();
            RetrieveMessageFromDeadLetterForSubscription();
        }
        public static void RetrieveMessageFromDeadLetterForQueue()
        {
            var receiverFactory = MessagingFactory.Create(
                 "sb://<ServiceBusNamespaceName>.servicebus.windows.net/",
                 new MessagingFactorySettings
                 {
                     TokenProvider = TokenProvider.CreateSharedAccessSignatureTokenProvider("RootManageSharedAccessKey", "<NamespaceLevelKey>"),
                     NetMessagingTransportSettings = { BatchFlushInterval = new TimeSpan(0, 0, 0) }
                 });
            string data = QueueClient.FormatDeadLetterPath("<QueueName>");
            var receiver = receiverFactory.CreateMessageReceiver(data);
            receiver.OnMessageAsync(
            async message =>
            {
                var body = message.GetBody<Stream>();
                lock (Console.Out)
                {
                    Console.WriteLine(message.MessageId);
                }
                await message.CompleteAsync();
            },
            new OnMessageOptions { AutoComplete = false, MaxConcurrentCalls = 1 });
        }
        public static void RetrieveMessageFromDeadLetterForSubscription()
        {
            var receiverFactory = MessagingFactory.Create(
                 "sb://<NS>.servicebus.windows.net/",
                 new MessagingFactorySettings
                 {
                     TokenProvider = TokenProvider.CreateSharedAccessSignatureTokenProvider("RootManageSharedAccessKey", "<NamespaceLevelSASKey>"),
                     NetMessagingTransportSettings = { BatchFlushInterval = new TimeSpan(0, 0, 0) }

                 });

            string data = SubscriptionClient.FormatDeadLetterPath("<TopicName>", "<SubscriptionName>");

            var receiver = receiverFactory.CreateMessageReceiver(data);

            receiver.OnMessageAsync(

            async message =>
            {
                var body = message.GetBody<Stream>();
                lock (Console.Out)
                {
                    Console.WriteLine("Message ID :" + message.MessageId);
                }
                await message.CompleteAsync();
            },
            new OnMessageOptions { AutoComplete = false, MaxConcurrentCalls = 1 });
        }
    }

 


 


 

Brought to you by Dr. Ware, Microsoft Office 365 Silver Partner, Charleston SC.