Reputation: 523
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 class appears to only be used to create a subscription. I don't see a way to retrieve a SubscriptionDescription object for an existing subscription.
Upvotes: 17
Views: 8748
Reputation: 2540
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;
Upvotes: 9
Reputation: 186
Microsoft.Azure.ServiceBus library is deprecated now in favor of Azure.Messaging.ServiceBus. So now this can be achieved with Azure.Messaging.ServiceBus.Administration.ServiceBusAdministrationClient:
var client = new Azure.Messaging.ServiceBus.Administration.ServiceBusAdministrationClient("connetionString");
var runtimeProps = (await client.GetQueueRuntimePropertiesAsync("queueName")).Value;
var messageCount = runtimeProps.ActiveMessageCount;
Upvotes: 4
Reputation: 523
I found what I was looking for:
var namespaceManager = NamespaceManager.CreateFromConnectionString(connectionString);
var subscriptionDesc = namespaceManager.GetSubscription(topicPath, subscriptionName);
long messageCount = subscriptionDesc.MessageCount;
Upvotes: 24