如何在 Azure 服务总线中从死信中查看和删除消息



我创建了一个 Azure 服务总线主题应用程序,该应用程序以死信形式查看所有消息。我偷看的一些特定消息(具有特定的消息 ID)需要从死信队列中删除。请为实现此目的提供帮助。

通过调用 complete 引用从死信队列中收到的中转消息,可以将其从死信队列中删除。

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

首先,如果需要知道如何创建服务总线主题和订阅:

  • 如何使用服务总线主题和订阅

若要从订阅接收消息,需要创建一个消息接收器:

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

然后,您可以只收听到达的消息:

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

此代码可帮助你删除 Azure 服务总线中的死信消息。

MessageReceiver fromQueueClient = null;
        MessagingFactory factory = MessagingFactory.CreateFromConnectionString(connectionString);
        fromQueueClient = await factory.CreateMessageReceiverAsync(_entityName, ReceiveMode.PeekLock);
            BrokeredMessage _message = await fromQueueClient.ReceiveAsync(SequenceNumber);
                await _message.CompleteAsync();

最新更新