.NET Core Azure servicebusqueue number of messages

1.1k Views Asked by At

I am currently working on a .NET Core project where I use the Microsoft.Azure.Servicebus version 1.0 NuGet package found here: https://github.com/Azure/azure-service-bus

The problem I have is that I haven't found any method to get a queue's number of active messages. This used to be pretty easy with .NET framework using the ServicebusNamespace.NamespaceManager, referring to a queue and use the .ActiveMessageCount.

Is this possible in some other way in this library with .NET Core 1.1?

2

There are 2 best solutions below

4
On BEST ANSWER

The .NET Standard client (Microsoft.Azure.ServiceBus) is deliberately not providing management operations. It states that management operations should not be performed at run time. Management operations are extremely slow.

Is this possible in some other way in this library with .NET Core 1.1?

Yes, it is possible.

Instead of the NamespaceManager that was available with the old client (WindowsAzure.ServiceBus), there's a ServiceBus management library (Microsoft.Azure.Management.ServiceBus.Fluent)

You will need to do the following:

  • Authenticate using ServiceBusManager
  • Access the namespace you're interested in via ServiceBusManager.Namespaces
  • Filter out the entity you're interested in by locating it under ServiceBusManager.Namespaces.Queues/ServiceBusManager.Namespaces.Topics. For subscription you'll need to locate one via ITopic object.
  • Once you've got your entity (IQueue, ITopic, or ISubscription), you'll be able to access the message counts.

I'm not a big fan of this approach. Rather than each developer reinventing this wheel, Azure Service Bus team should have provided a helper library to replace NamespaceManger. You can always raise an issue or vote for an issue that was closed.

Management operations were introduced back in version 3.1.1 with pull request #481.

0
On

It is now possible using the latest version of the Service Bus library (3.1.1):

using Microsoft.Azure.ServiceBus;
using Microsoft.Azure.ServiceBus.Management;

var client = new ManagementClient(connectionString);
var queue = await client.GetQueueRuntimeInfoAsync(queuePath);
var counts = queue.MessageCountDetails;

var subs = await client.GetSubscriptionRuntimeInfoAsync(topic, subscription);
var countForThisSubscription = subs.MessageCount;  //// (Comes back as a Long.)