Reputation: 21
I Have 9 messages in my currente Queue, I wish to get all messages and return in a List, I also tried the code below:
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?
I am using .net core!!
Upvotes: 0
Views: 1325
Reputation: 25994
You're using OnMessage API, where register a callback with message handler provided by the SDK. You will be always given one message per callback invocation and this is supposed to be your message pump.
If you need to fetch several messages in a single operation (a batch), you should use ReceiveAsync()
overload that accepts number of requested messages, using either a specific client or the general MessageReceiver
class.
Upvotes: 1