4

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?

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131

2 Answers2

8

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.)
Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
malafreniere
  • 156
  • 2
  • 3
5

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.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Sean Feldman
  • 23,443
  • 7
  • 55
  • 80
  • Hi Sean, thank you for an excellent answer. I really miss the precious approach but also hope that this might be changed and simpler in the future. – Nicklas Lundgren Sep 09 '17 at 09:37
  • Your are welcome Nicklas. If enough people chime in to let know there should be an abstraction, perhaps ASB team will add another package to address it. Alternative would be a community driven one. GitHub issue tracker is the best way too let know something should be done about it. – Sean Feldman Sep 09 '17 at 15:57
  • During the waiting time, you can use the Mossharbor code, this code provide a way simple to management features that you need in SB https://github.com/Mossharbor/AzureWorkArounds.ServiceBus – Roberto Borges Jun 13 '18 at 16:12
  • Waiting time should be short - there's already a [PR](https://github.com/Azure/azure-service-bus-dotnet/pull/481) in place :) – Sean Feldman Jun 13 '18 at 16:57