I want to get the count of the messages from an Azure Service Bus Queue, using the SessionId of the messages.
I found how to count them without filtering by Session IDs here - Get queue message count using Microsoft.Azure.Management.ServiceBus, but I really need to count the messages with a specific SessionId.
1 Answer
You can get the count of the messages from an Azure Service Bus Queue as said, but by using the particular SessionId you cannot get the messages count.
The Azure.Messaging.ServiceBus. ServiceBusSessionReceiver is responsible for receiving ServiceBusReceivedMessage and settling messages from session-enabled Queues and Subscriptions.
public class ServiceBusSessionReceiver :
Azure.Messaging.ServiceBus.ServiceBusReceiver
By using the default Properties of this class, we can get the details of SessionId and others like PrefetchCount, IsClosed etc..
ServiceBusReceiver.PeekMessagesAsync(maxMessages)(see MS doc), but you would have to provide the number of messages to be peeked (which may not be suitable for your use case?). I'm not aware of another solution than to peek due to the receive&delete or peek lock behavior of service bus queues.