how to peek and delete a message from deadletter i

2019-09-09 20:49发布

问题:

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.

回答1:

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



回答2:

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

  • How to use Service Bus topics and subscriptions

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());


回答3:

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();