"A simple console app can be very helpful to you in achieving your goal of viewing the dead letter messages in your Service Bus Queue or Topic Subscription. The only thing you need to do is to receive the messages from the dead letter path of your Queue or Topic Subscription in peeklock mode and Display the required message details.
Here is a code to simple console app to display the deadletter messages.
using System;
using System.Threading.Tasks;
using Microsoft.ServiceBus.Messaging;
namespace DeadLetterQueue
{
class Program
{
/*Supply the connection string of your Service Bus Namespace here*/
const string connectionString = "connection string of your Service Bus Namespace";
/*Supply the Name of your Service Bus Entity */
const string entityName = "Entity Name";
/*Supply the Number of deadletter messages you need to retrieve from your Entity here*/
const int numberOfMessages = 5;
static void Main(string[] args)
{
ViewDeadLetterMessages().GetAwaiter().GetResult();
Console.ReadKey();
}
static async Task ViewDeadLetterMessages()
{
MessagingFactory messageFactory = MessagingFactory.CreateFromConnectionString(connectionString);
Console.WriteLine(""DeadLetter Messages of {0}"", entityName);
//Getting the deadletter path of the Service Bus Entity
string _path = QueueClient.FormatDeadLetterPath(queueName);
for (int i = 0; i < numberOfMessages; i++)
{
var queueClient = await messageFactory.CreateMessageReceiverAsync(_path, ReceiveMode.PeekLock);
BrokeredMessage _message = await queueClient.ReceiveAsync();
Console.WriteLine(""MessageId Message {0} - {1} "", i, _message.MessageId);
_message.Complete();
_message.Abandon();
}
}
}
}