Is there a way to get the current message count for an Azure topic subscription?
I see that the SubscriptionDescription class has a MessageCount property, but this
I found what I was looking for:
var namespaceManager = NamespaceManager.CreateFromConnectionString(connectionString);
var subscriptionDesc = namespaceManager.GetSubscription(topicPath, subscriptionName);
long messageCount = subscriptionDesc.MessageCount;
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.