如何清除来自 Azure 服务总线的待处理消息?

How to clear pending message from azure service bus?

我正在使用 azure 服务总线接收消息以处理我的后台进程。在我的后台进程中,我想从 azure 服务总线中清除已处理的消息。有什么方法可以清除 azure 服务总线消息吗???

阅读 How to receive messages from a queue 并确保使用 _queueClient.Complete()_queueClient.Abandon() 完成每条消息。

您可以使用 "Microsoft.ServiceBus.Messaging" 并按入队时间清除消息。接收消息,按 ScheduledEnqueueTime 过滤,并在消息在特定时间入队时执行清除。

using Microsoft.ServiceBus.Messaging;

MessagingFactory messagingFactory = MessagingFactory.CreateFromConnectionString(connectionString);

var queueClient = messagingFactory.CreateQueueClient(resourceName, ReceiveMode.PeekLock);

var client = messagingFactory.CreateMessageReceiver(resourceName, ReceiveMode.PeekLock);

BrokeredMessage message = client.Receive();

if (message.EnqueuedTimeUtc < MessageEnqueuedDateTime)
{
    message.Complete(); 
}