I know there is a way to determine the number of messages (or approximate number) in the Azure Queue (Store Account); however is there a way to query for the number of pending messages on an Azure Service Bus queue?
-
See also http://stackoverflow.com/questions/18283583/azure-service-bus-queue-count – SteveC Aug 03 '15 at 16:41
10 Answers
var nsmgr = Microsoft.ServiceBus.NamespaceManager.CreateFromConnectionString(connectionString);
long count = nsmgr.GetQueue(queueName).MessageCount;

- 351
- 4
- 4
-
12With the new `Microsoft.Azure.ServiceBus` library, this is done a little differently. You can see how it's done in the [ManagementUnitTests#GetQueueRuntimeInfoTest()](https://github.com/Azure/azure-service-bus-dotnet/blob/master/test/Microsoft.Azure.ServiceBus.UnitTests/Management/ManagementClientTests.cs#L262). – Ehtesh Choudhury Sep 18 '18 at 23:28
-
I'm using .net core. nuget Microsoft.ServiceBus do not have NamespaceManager – Neo Feb 09 '20 at 18:07
-
-
1This is no longer the best answer in 2022 - for the newer Azure.Messaging.ServiceBus library [this answer](https://stackoverflow.com/a/72543295/5740181) is most up-to-date. – Daniel Elkington Dec 05 '22 at 23:49
It is called MessagesCountDetails.ActiveMessageCount. It returns the number of the Active Messages in the Queue. You probably have some Dead letter messages:
var msg = Microsoft.ServiceBus.NamespaceManager.CreateFromConnectionString(Settings.Default.ConnectionString);
numofmessages.Text = msg.GetQueue(QueueName).MessageCountDetails.ActiveMessageCount.ToString();

- 24,352
- 18
- 113
- 198

- 364
- 1
- 4
- 12
-
1That child-object (MessageCountDetails) was hiding all the goodies! Microsoft.ServiceBus.Messaging.QueueDescription qd = nsm.GetQueue(qName); int x = 0; x = qd.MessageCountDetails.ActiveMessageCount; x = qd.MessageCountDetails.DeadLetterMessageCount; x = qd.MessageCountDetails.ScheduledMessageCount; x = qd.MessageCountDetails.TransferDeadLetterMessageCount; x = qd.MessageCountDetails.TransferMessageCount; – granadaCoder Feb 16 '17 at 20:57
-
I'm using .net core. nuget Microsoft.ServiceBus do not have NamespaceManager – Neo Feb 09 '20 at 18:07
Correct answer as of 2020+
Use of new packages as follows:
<PackageReference Include="Azure.Messaging.ServiceBus" Version="x.x.x" />
also two namespaces in the same package
using Azure.Messaging.ServiceBus;
using Azure.Messaging.ServiceBus.Administration;
and then you can use the new class ServiceBusAdministrationClient
var administrationClient = new ServiceBusAdministrationClient("connectionString");
var props = await administrationClient.GetQueueRuntimePropertiesAsync("queue");
var messageCount = props.Value.ActiveMessageCount;

- 8,338
- 14
- 57
- 95
-
1This answer should really now be the accepted one given that Microsoft now recommends devs use the "Azure.Messaging.ServiceBus" package and not the older "Microsoft.Azure.ServiceBus" package which **depreciated in Nov 2020**. – bytedev Sep 23 '22 at 06:59
-
have you looked at the Queue Description API? There's a property called MessageCount
.
Here's the .NET SDK reference documentation page as well.

- 69,407
- 21
- 141
- 189
Based off what Joseph had as an answer I came up with, but for Topics and Subscriptions.
public async Task<long> GetCounterMessages()
{
var client = new ManagementClient(ServiceBusConnectionString);
var subs = await client.GetSubscriptionRuntimeInfoAsync(TopicName, SubscriptionName);
var countForThisSubscription = subs.MessageCount; //// (Comes back as a Long.)
return countForThisSubscription;
}

- 1,014
- 3
- 18
- 50
I ran into this same problem trying to get the count from the dead letter queue. It looks like the deadletterqueue doesn't allow you to get a count directly, you get it from the MessageCountDetails of the normal Queue.
string connectionString = ConfigurationManager.AppSettings["Microsoft.ServiceBus.Connstr"].ToString();
NamespaceManager nsmgr = Microsoft.ServiceBus.NamespaceManager.CreateFromConnectionString(connectionString);
return nsmgr.GetQueue(QueueName).MessageCountDetails.DeadLetterMessageCount;

- 15,808
- 23
- 102
- 173

- 19
- 3
-
I'm using .net core. nuget Microsoft.ServiceBus do not have NamespaceManager – Neo Feb 09 '20 at 18:07
Here's a PowerShell example to continually eyeball the queue length as used in Azure Portal Cloud Shell
cd "Azure:\<MySubscription>\"
while (1) {(Get-AzureRmServiceBusQueue -ResourceGroup <myRG> -NamespaceName <myNS> -QueueName <myQueueName>).CountDetails | Select -expand ActiveMessageCount}

- 956
- 12
- 22
I've spent good 2 hours digging through docs to get that and for people using .net core and Microsoft.Azure.ServiceBus nuget package, code looks like that:
var managementClient = new ManagementClient("queue connection string"));
var runtimeInfo = await managementClient.GetQueueRuntimeInfoAsync("queueName");
var messagesInQueueCount = runtimeInfo.MessageCountDetails.ActiveMessageCount;
Aparently you get the information about all Counts(including deadletter, active, etc.) from QueueRuntimeInfo object instead of old QueueDescription object.

- 1
As per the recommendation by Microsoft, it is recommended to use Microsoft.Azure.ServiceBus in which you can easily fetch the message count by
var managementClient = new ManagementClient("connection string for queue");
var queue = await managementClient.GetQueueRuntimeInfoAsync("queue name");
var messages = queue.MessageCount;

- 81
- 1
- 7
Also..you can check the pending messages on Azure Management Portal...on the dashboard for service bus queue...under quick glance...you can see the queue length...this is the number of current/pending messages in length at the time of dashboard page load.

- 59
- 4
-
2You can also use the service bus explorer in Visual Studio (if you have it) to view the properties of each entity for a namespace. – TheDude Apr 28 '13 at 19:00