how to peek and delete a message from deadletter in azureservicebus

3.7k Views Asked by At

I have created an azure service bus topic application which peek all messages in deadletter. Some specific messages(with particular messageid) which i peeked need to be removed from the deadletter queue. Please provide help for implementing this.

3

There are 3 best solutions below

0
On

This code helps you to delete a dead-letter message in Azure service bus.

MessageReceiver fromQueueClient = null;

        MessagingFactory factory = MessagingFactory.CreateFromConnectionString(connectionString);
        fromQueueClient = await factory.CreateMessageReceiverAsync(_entityName, ReceiveMode.PeekLock);

            BrokeredMessage _message = await fromQueueClient.ReceiveAsync(SequenceNumber);

                await _message.CompleteAsync();
0
On

First if you need to know how to create a service bus topic and subscription:

To receive message from a subscription, you need to create a message receiver :

//Create the messaging factory
var messagingFactory = MessagingFactory.CreateFromConnectionString("ServiceBusConnectionString");

// Get the dead letter path
var deadLetterPath = SubscriptionClient.FormatDeadLetterPath("TopicPath", "subscriptionName");

// Get the message receiver for the deal letter queue.
var messageReceiver = messagingFactory.CreateMessageReceiver(deadLetterPath);

Then you can just listen for messages arriving:

// This is the list of ids that need to be delete
var messageIdsToDelete = new List<long>(...);
messageReceiver.OnMessage((message) =>
{
    // Check if we have to delete the message
    if (messageIdsToDelete.Contains(message.SequenceNumber))
    {
        // Complete and delete the message from the queue.
        message.Complete();
    }

}, new OnMessageOptions());
1
On

By calling complete on the reference to the brokered message you receive from the dead letter queue you can remove it from the dead letter queue.

https://msdn.microsoft.com/library/azure/microsoft.servicebus.messaging.brokeredmessage.complete.aspx