Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

767
Views
Azure Service Bus: ReceiveMessagesAsync returns only a subset

I wrote a code to read 1000 messages one shot from an Azure Service Bus queue. I read the messages with the line: await receiver.ReceiveMessagesAsync(1000); but only a subset of the messages are received.

I took the code from the sample: https://github.com/Azure/azure-sdk-for-net/blob/main/sdk/servicebus/Azure.Messaging.ServiceBus/tests/Samples/Sample01_HelloWorld.cs, the SendAndReceiveMessageSafeBatch() method

This is my code:

public class Program
{
    static void Main(string[] args)
    {
        SendAndReceiveMessage().GetAwaiter().GetResult();
    }

    public static async Task SendAndReceiveMessage()
    {
        var connectionString = "myconnectionstring";
        var queueName = "myqueue";

        // since ServiceBusClient implements IAsyncDisposable we create it with "await using"
        await using var client = new ServiceBusClient(connectionString);

        // create the sender
        var sender = client.CreateSender(queueName);

        IList<ServiceBusMessage> messages = new List<ServiceBusMessage>();
        for (var i = 0; i < 1000; i++)
        {
            messages.Add(new ServiceBusMessage($"Message {i}"));
        }

        // send the messages
        await sender.SendMessagesAsync(messages);

        // create a receiver that we can use to receive the messages
        var options = new ServiceBusReceiverOptions()
        {
            ReceiveMode = ServiceBusReceiveMode.ReceiveAndDelete
        };

        ServiceBusReceiver receiver = client.CreateReceiver(queueName, options);

        // the received message is a different type as it contains some service set properties
        IReadOnlyList<ServiceBusReceivedMessage> receivedMessages = await receiver.ReceiveMessagesAsync(1000);

        Console.WriteLine($"Received {receivedMessages.Count} from the queue {queueName}");

        foreach (ServiceBusReceivedMessage receivedMessage in receivedMessages)
        {
            var body = receivedMessage.Body.ToString();
            Console.WriteLine(body);
        }

        Console.WriteLine("END");
        Console.ReadLine();
    }
}

Do you have any suggestion how to read all 1000 messages one shot?

over 4 years ago · Santiago Trujillo
1 answers
Answer question

0

This is expected behaviour with Azure Service Bus. The number of messages to receive, maxMessages is a maximum number that is not guaranteed.

over 4 years ago · Santiago Trujillo Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!