0
votes

I have an Azure Service Bus queue with 236 messages in the dead-letter sub-queue. I know this because the Azure Portal and Service Bus Explorer show me that number.

I wrote the following in LINQPad to retrieve all 236 messages and examine them. However it only ever returns 136 of the 236 messages.

I'm using v5.1.0 of the Microsoft.Azure.ServiceBus nuget package.

Any thoughts as to why I'm not getting all 236 messages?

const string queueName = "the-queue-name";
const string serviceBusConnectionString = "Endpoint=...";

var receiver = new MessageReceiver(serviceBusConnectionString, EntityNameHelper.FormatDeadLetterPath(queueName));
var receivedMessages = await receiver.PeekAsync(300);

Console.WriteLine($"Received [{receivedMessages.Count()}] dead-letters");
2

2 Answers

2
votes

There may be two reasons for the Peek operation to not retrieve exact count you specififed:

One reason is that the aggregated size of the collection of messages exceeds the maximum size of 256 KB. Another reason is that if the queue or topic has the EnablePartitioning property set to true, a partition may not have enough messages to complete the requested number of messages.

So generally what I would suggest you is to use Peek operation repeatedly, probably using a loop. Say you received 136 out of 236 messages when you performed PeekAsync(236). In this case, you will have to note down the count you have actually received and call the PeekAsync(236 - returned count), which has the parameter with the subtracted value. returned count should be incremented everytime you get the result.

The client object that you are using remembers the last peeked sequence number and then it continues to retrieve the next set of messages. This way you can retrieve all the messages.

1
votes

Peeking messages is never guaranteed to retrieve exactly the number of the messages you've requested. That's the maximum number of messages you'll get back. In the latest SDK (version 7) the parameter passed into PeekMessagesAsync method is called maxMessages and is documented as:

The maximum number of messages that will be fetched.

A common solution is indeed to loop until you get the desired number of messages or operate on the batches of different sizes if the code allows that.