在.net 5中从Azure服务总线队列获取消息id



我想发送一条消息到服务总线队列并接收它的ID。

sdk示例似乎提供了一个解决方案。

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 the sender
ServiceBusSender sender = client.CreateSender(queueName);
// create a message that we can send. UTF-8 encoding is used when providing a string.
ServiceBusMessage message = new ServiceBusMessage("Hello world!");
// send the message
await sender.SendMessageAsync(message);
// create a receiver that we can use to receive 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();
string id = receivedMessage.Id

ReceiveMessageAsync()将返回队列中的第一个项目,因此我如何确保在发送消息

之间不会有另一个消息到达队列(来自另一个客户端)
await sender.SendMessageAsync(message);

和接收

await receiver.ReceiveMessageAsync();

您可以通过MessageId属性设置给定ServiceBusMessage的标识符:

消息标识符是应用程序定义的值,它唯一地标识消息及其有效负载。标识符是一个自由格式的字符串,可以反映GUID或从应用程序上下文派生的标识符。如果启用,重复检测特性识别并删除具有相同MessageId的第二次和进一步提交的消息。

只需将标识符定义为GUID并保存该值。不需要额外的复杂性。

更多信息:

ServiceBusMessage。即MessageId属性

相关内容

  • 没有找到相关文章

最新更新