Azure Service Bus只返回一条消息

时间:2017-06-01 18:36:54

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

我的currente队列中有9条消息,我希望获取所有消息并返回列表,我也尝试了下面的代码:

List<MessageHandler> messages = new List<MessageHandler>();
         MessageHandler _message = new MessageHandler();
         string body = string.Empty;
         if (deleteMessagesOnReceive)
            client = new QueueClient(ServiceBusConnectionString, QueueName, ReceiveMode.ReceiveAndDelete);
         else
            client = new QueueClient(ServiceBusConnectionString, QueueName, ReceiveMode.PeekLock);


         client.RegisterMessageHandler
         (
             async (message, token) =>
             {
                _message.MessageSequenceNumber = message.SystemProperties.SequenceNumber;
                _message.MessageBody = Encoding.UTF8.GetString(message.Body);
                _message.DateExpire = message.ExpiresAtUtc;
                messages.Add(_message);
                await client.CompleteAsync(message.SystemProperties.LockToken);
             }
         );

         return messages;

but always return 1 message in random order.

There's some way to pick all messages?

我正在使用.net核心!!

1 个答案:

答案 0 :(得分:1)

您正在使用OnMessage API,其中使用SDK提供的消息处理程序注册回调。每次回调调用时,您将始终收到一条消息,这应该是您的消息泵。

如果您需要在单个操作(批处理)中获取多个消息,则应使用ReceiveAsync()重载,该重载接受多个请求的消息,使用特定客户端或常规r.close()类。