Search code examples
c#azureazureservicebusazure-servicebus-queues

Get queue message count using Microsoft.Azure.Management.ServiceBus


I need an example on how to make CRUD operations on service bus queues.

I actually need an instance of Microsoft.Azure.Management.ServiceBus.SBQueue class, so I can count the messages in that queue.


Solution

  • The older way is to use the ManagementClient

    var managementClient = new ManagementClient(connectionString);
    var queueRuntimeInfo = await managementClient.GetQueueRuntimeInfoAsync(queueName);
    Console.WriteLine(queueRuntimeInfo.MessageCount);
    

    The more modern way is to use ServiceBusAdministrationClient

    var client = new ServiceBusAdministrationClient(connectionString);
    var runtimeProperties = await client.GetQueueRuntimePropertiesAsync(queueName);
    Console.WriteLine(runtimeProperties .ActiveMessageCount);