azureazureservicebusazure-servicebus-topicsazure-servicebus-subscriptions

Azure service bus read all messages at once using Azure.Messaging.ServiceBus


I am using Azure.Messaging.ServiceBus nuget package to work with Azure service bus. We have created a topic and a subscription. The subscription has 100+ messages. We want to read all the message and continue to read message as they arrive.

Microsoft.Azure.ServiceBus package (deprecated now) provided RegisterMessageHandler which use to process every incoming message. I am not able to find similar option under Azure.Messaging.ServiceBus nuget package.

I am able to read one message at a time but I have to call await receiver.ReceiveMessageAsync(); every time manually.


Solution

  • To receive multiple messages (a batch), you should use ServiceBusReceiver.ReceiveMessagesAsync() (not plural, not singular 'message'). This method will return whatever number of messages it can send back. To ensure you retrieve all 100+ messages, you'll need to loop until no messages are available.

    If you'd like to use a processor, that's also available in the new SDK. See my answer to a similar question here.