首页 > 解决方案 > Azure 服务总线使用 .net sdk 按 ID 删除队列消息

问题描述

我有一个充满消息的 Azure 服务总线队列,我想根据它的 ID 删除一条消息。

REST API 通过 http 请求提供此解决方案:

DELETE  http{s}://{serviceNamespace}.servicebus.windows.net/{queuePath}/messages/{messageId}/{lockToken}

对于 .net 5 sdk,微软提供了一个解决方案,不使用消息的 id,而是使用 receivedMessage 对象

string connectionString = "<connection_string>";
string queueName = "<queue_name>";

// since ServiceBusClient implements IAsyncDisposable we create it with "await using"
await using var client = new ServiceBusClient(connectionString);

// create a receiver that we can use to receive and settle the message
ServiceBusReceiver receiver = client.CreateReceiver(queueName);

// the received message is a different type as it contains some service set properties
ServiceBusReceivedMessage receivedMessage = await receiver.ReceiveMessageAsync();

// complete the message, thereby deleting it from the service
await receiver.CompleteMessageAsync(receivedMessage);

我想使用 .net sdk 通过它的 id 删除一条消息。

标签: c#azure.net-5azure-servicebus-queues

解决方案


不幸的是,没有办法用一个简单的命令来实现它。这里讨论了其中的一些内容。您可以将您的声音添加到线程中,并希望它能够传达给正确的受众。


推荐阅读