Azure 服务总线 "ReceiveAsync"
Azure Service Bus "ReceiveAsync"
有没有办法使用 Microsoft.Azure.ServiceBus
程序包在当前线程上等待从队列接收消息?
这可能更多地是我的理解问题,并且希望以不打算使用的方式使用该技术,但我想做的是结合 [=13] 中的发送和接收示例=] 这样您就可以将消息发送到各种队列,并能够收听和处理 "replies" (只是您正在收听队列中的消息)并在完成后关闭连接正在接收消息。
这里是一些伪代码:
// send message(s) that will be consumed by other processes / applications, and by doing so later on we will expect some messages back
await SendMessagesAsync(numberOfMessages);
var receivedMessages = 0;
while (receivedMessages < numberOfMessages)
{
// there is no "ReceiveAsync" method, this is what I would be looking for
Message message = await queueClient.ReceiveAsync(TimeSpan.FromSeconds(30));
receivedMessages++;
// do something with the message here
}
await queueClient.CloseAsync();
这可能还是我 "doing it wrong"?
在新库中 ReceiveAsync
方法可用 MessageReceiver
class:
var messageReceiver = new MessageReceiver(SBConnString, QueueName, ReceiveMode.PeekLock);
Message message = await messageReceiver.ReceiveAsync();
在Microsoft.Azure.ServiceBus
库中,没有ReceiveAsync
这个东西。在此,您可以使用 RegisterOnMessageHandlerAndReceiveMessages()
处理或接收消息。有了这个,您可以接收带有事件的消息。有了这个 RegisterOnMessageHandlerAndReceiveMessages()
就像 queueClient.RegisterMessageHandler(ReceiveOrProcessMessagesAsync, messageHandlerOptions);
并且你必须为 receiveMessages 单独创建这个事件,在我们的例子中它是 ReceiveOrProcessMessagesAsync
static async Task ReceiveOrProcessMessagesAsync(Message message, CancellationToken token)
{
// Process the message
Console.WriteLine($"Received message: SequenceNumber:{message.SystemProperties.SequenceNumber} Body:{Encoding.UTF8.GetString(message.Body)}");
// Complete the message so that it is not received again.
// This can be done only if the queueClient is created in ReceiveMode.PeekLock mode (which is default).
await queueClient.CompleteAsync(message.SystemProperties.LockToken);
// Note: Use the cancellationToken passed as necessary to determine if the queueClient has already been closed.
// If queueClient has already been Closed, you may chose to not call CompleteAsync() or AbandonAsync() etc. calls
// to avoid unnecessary exceptions.
}
你参考下面的link了解Microsoft.Azure.ServiceBus
https://docs.microsoft.com/en-us/azure/service-bus-messaging/service-bus-dotnet-get-started-with-queues
有没有办法使用 Microsoft.Azure.ServiceBus
程序包在当前线程上等待从队列接收消息?
这可能更多地是我的理解问题,并且希望以不打算使用的方式使用该技术,但我想做的是结合 [=13] 中的发送和接收示例=] 这样您就可以将消息发送到各种队列,并能够收听和处理 "replies" (只是您正在收听队列中的消息)并在完成后关闭连接正在接收消息。
这里是一些伪代码:
// send message(s) that will be consumed by other processes / applications, and by doing so later on we will expect some messages back
await SendMessagesAsync(numberOfMessages);
var receivedMessages = 0;
while (receivedMessages < numberOfMessages)
{
// there is no "ReceiveAsync" method, this is what I would be looking for
Message message = await queueClient.ReceiveAsync(TimeSpan.FromSeconds(30));
receivedMessages++;
// do something with the message here
}
await queueClient.CloseAsync();
这可能还是我 "doing it wrong"?
在新库中 ReceiveAsync
方法可用 MessageReceiver
class:
var messageReceiver = new MessageReceiver(SBConnString, QueueName, ReceiveMode.PeekLock);
Message message = await messageReceiver.ReceiveAsync();
在Microsoft.Azure.ServiceBus
库中,没有ReceiveAsync
这个东西。在此,您可以使用 RegisterOnMessageHandlerAndReceiveMessages()
处理或接收消息。有了这个,您可以接收带有事件的消息。有了这个 RegisterOnMessageHandlerAndReceiveMessages()
就像 queueClient.RegisterMessageHandler(ReceiveOrProcessMessagesAsync, messageHandlerOptions);
并且你必须为 receiveMessages 单独创建这个事件,在我们的例子中它是 ReceiveOrProcessMessagesAsync
static async Task ReceiveOrProcessMessagesAsync(Message message, CancellationToken token)
{
// Process the message
Console.WriteLine($"Received message: SequenceNumber:{message.SystemProperties.SequenceNumber} Body:{Encoding.UTF8.GetString(message.Body)}");
// Complete the message so that it is not received again.
// This can be done only if the queueClient is created in ReceiveMode.PeekLock mode (which is default).
await queueClient.CompleteAsync(message.SystemProperties.LockToken);
// Note: Use the cancellationToken passed as necessary to determine if the queueClient has already been closed.
// If queueClient has already been Closed, you may chose to not call CompleteAsync() or AbandonAsync() etc. calls
// to avoid unnecessary exceptions.
}
你参考下面的link了解Microsoft.Azure.ServiceBus
https://docs.microsoft.com/en-us/azure/service-bus-messaging/service-bus-dotnet-get-started-with-queues