【问题标题】:Azure Service Bus "ReceiveAsync"Azure 服务总线“ReceiveAsync”
【发布时间】:2018-10-30 16:39:27
【问题描述】:

有没有办法使用Microsoft.Azure.ServiceBus 包来等待当前线程接收来自队列的消息?

这可能更多是我的理解和希望以不打算使用的方式使用该技术的问题,但我想做的是结合来自the following Microsoft example 的发送和接收示例,以便您可以将消息发送到各种队列,并能够监听和处理“回复”(只是您在队列中收听的消息)并在接收完消息后关闭连接。

这里有一些伪代码:

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

这可能还是我“做错了”?

【问题讨论】:

标签: c# azure azureservicebus azure-servicebus-queues


【解决方案1】:

在新库中ReceiveAsync 方法可用于MessageReceiver 类:

var messageReceiver = new MessageReceiver(SBConnString, QueueName, ReceiveMode.PeekLock);
Message message = await messageReceiver.ReceiveAsync();

Get started sending and receiving messages from Service Bus queues using MessageSender and MessageReceiver查看完整示例。

【讨论】:

  • 推荐哪一个 foe azure functions ?你的 ans 或以下ReceiveOrProcessMessagesAsync ans?请指导?
  • Azure Functions 服务总线触发器会为您处理消息的接收,因此您不需要任何这些。还是您没有使用触发器?
【解决方案2】:

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.
    }

您可以参考以下链接了解Microsoft.Azure.ServiceBus https://docs.microsoft.com/en-us/azure/service-bus-messaging/service-bus-dotnet-get-started-with-queues

【讨论】:

  • 推荐哪一个 foe azure functions ?您的 ans 或以下 ReceiveOrProcessMessagesAsync ans?请指导?
  • 要获取Azure函数的调用日志吗?
  • 对于 Azure Functions,安装 Microsoft.NET.Sdk.Functions。此包包含在 Azure Functions 上执行的所有必要操作
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2022-01-09
  • 2021-12-30
  • 2014-10-20
  • 2012-10-03
相关资源
最近更新 更多