How do you get the message count for Azure Topic Subscription?
The accepted answer is for when using the .NET Framework library with the namespace Microsoft.ServiceBus.Messaging
(nuget package).
For the .NET Standard library with the namespace Microsoft.Azure.ServiceBus
(nuget package) the following code does the trick:
var managementClient = new ManagementClient(connectionString);
var runTimeInfo = await managementClient.GetSubscriptionRuntimeInfoAsync(topicPath, subscriptionName);
var messageCount = runTimeInfo.MessageCountDetails.ActiveMessageCount;
See Microsoft.ServiceBus.Messaging vs Microsoft.Azure.ServiceBus for more details about the differences between the two libraries.
With the retirement of .NET Standard there is a new namespace for .NET 5+ apps, Azure.Messaging.ServiceBus
(nuget package). The code required to do the same with this package is:
var client = new Azure.Messaging.ServiceBus.Administration.ServiceBusAdministrationClient("connetionString");
var runtimeProps = (await client.GetQueueRuntimePropertiesAsync("queueName")).Value;
var messageCount = runtimeProps.ActiveMessageCount;
I found what I was looking for:
var namespaceManager = NamespaceManager.CreateFromConnectionString(connectionString);
var subscriptionDesc = namespaceManager.GetSubscription(topicPath, subscriptionName);
long messageCount = subscriptionDesc.MessageCount;