MessageReceiver.ReceiveBatch() not working as intended

1.1k Views Asked by At

I am trying to receive messages in batch from the ServiceBus using the ReceiveBatch method in the MessageReceiver:

IEnumerable<BrokeredMessage> messages;
var messagingfactory = MessagingFactory.CreateFromConnectionString("ConnectionString");
var msgrcvr = messagingfactory.CreateMessageReceiver("queueName", ReceiveMode.ReceiveAndDelete);
messages = msgrcvr.ReceiveBatch(20, timeoutInSecs);

I have checked that my queue contains 20 messages using the Service Bus Explorer.

This code returns only one message in the messages structure. Is there some property I am missing?

1

There are 1 best solutions below

3
On

This is only a partial-answer or work-around; the following code reliably gets all elements, but doesn't use the "ReceiveBatch"; note, as far as I can discern, Peek(i) operates on a one-based index. Also: depending on which server one is running on, if you are charged by the message pull, this may (or may not) be more expensive, so use at your own risk:

        List<BrokeredMessage> dlIE = new List<BrokeredMessage>();

        BrokeredMessage potentialMessage = null;
        int loopCount = 1; 
        while ((potentialMessage = deadletterSubscriptionClient.Peek(loopCount)) != null) 
        { 
             dlIE.Add(potentialMessage); loopCount++; 
        }