3

The old method of getting count from Queue was like this:

 CloudQueue q = queueClient.GetQueueReference(QUEUE_NAME);
            q.FetchAttributes();
            qCnt = q.ApproximateMessageCount;

This no longer works with .netCore with AMQP 1.0

I am not finding a good way to get a message count. Any ideas on what I am missing ?

w2olves
  • 2,229
  • 10
  • 33
  • 60
  • This doesn't look Azure Service Bus code snippet, but Storage queues. – Sean Feldman May 10 '17 at 22:46
  • Sure, what is the equivalent function for getting the number of items in Queue in ServiceBus ? – w2olves May 11 '17 at 01:13
  • Google "azure service bus queue get message count" and literally the very first result is a SO answer to your question :D http://stackoverflow.com/questions/16254951/determining-how-many-messages-are-on-the-azure-service-bus-queue – Sean Feldman May 11 '17 at 06:19
  • The example you quote above applies to storage queues not servicebus, the answer below is the right one as it explains you need to get an oauth token to get the count. – w2olves May 11 '17 at 17:22
  • I'm sorry mate, but you have to read a bit closer. Microsoft.ServiceBus.NamespaceManager is not storage queues, but the Full .NET framework client for ASB. There are multiple ways to skin a cat: .NET Framework client using Namespace manager or Management library with AAD. You need a token if you go through AAD. If you have a connection string (containing SAS token), you don't need the AAD. – Sean Feldman May 11 '17 at 18:53

1 Answers1

3

There is a Microsoft.Azure.Management.ServiceBus Libary that is a preview version, is 100% compatible .Netcore. We can get more detail here.

Preparetion:

Registry Azure Active Directory application and assign Role

Steps:

Create a .net core console project and add the following code.

 var tenantId = "tenantid";
            var context = new AuthenticationContext($"https://login.windows.net/{tenantId}");
            var clientId = "Client";
            var clientSecret = "Secret";
            var subscriptionId = "subscriptionId";
            var result =  context.AcquireTokenAsync(
                "https://management.core.windows.net/",
                new ClientCredential(clientId, clientSecret)).Result;

            var creds = new TokenCredentials(result.AccessToken);
            var sbClient = new ServiceBusManagementClient(creds)
            {
                SubscriptionId = subscriptionId
            };
            var queueParams = new QueueCreateOrUpdateParameters()
            {
                Location = "East Asia",
                EnablePartitioning = true
            };
           
            var queue = sbClient.Queues.ListAll("groupname", "namespace").ToList().FirstOrDefault(x => x.Name.Equals("queuename")); 
            var messagecount = queue.MessageCount;

enter image description here

From Azure poratal, we check the message in the queue

enter image description here

Project.json file:

{
  "version": "1.0.0-*",
  "buildOptions": {
    "emitEntryPoint": true
  },

  "dependencies": {
    "Microsoft.Azure.Management.ServiceBus": "0.2.0-preview",
    "Microsoft.IdentityModel.Clients.ActiveDirectory": "3.13.9",
    "Microsoft.NETCore.App": {
      "type": "platform",
      "version": "1.0.1"
    }
  },

  "frameworks": {
    "netcoreapp1.0": {
      "imports": "dnxcore50"
    }
  }
}
Community
  • 1
  • 1
Tom Sun - MSFT
  • 24,161
  • 3
  • 30
  • 47
  • The above worked. For anyone else who stumbles upon this, groupname is your Resourcegroup name and NameSpace is the Queue Namespace. – w2olves May 11 '17 at 17:18
  • I am sending messages to the queue like this queueClient = new QueueClient(ServiceBusConnectionString, QueueName, ReceiveMode.PeekLock); // Create a new brokered message to send to the queue var message = new Message($"Message {precheckMessage}"); // Send the message to the queue await queueClient.SendAsync(message); Is there an easy way to get the QueueID when I push the message ? Like a context ? – w2olves May 11 '17 at 17:20
  • If using above code, we can get queueid easy `var queue = sbClient.Queues.ListAll("groupname", "namespace").ToList().FirstOrDefault(x => x.Name.Equals("queuename"));` `var id = queue.id` then we can get queue id as **/subscriptions/subscriptionId/resourceGroups/resource groupname/providers/Microsoft.ServiceBus/namespaces/servicebusname/queues/queuename** – Tom Sun - MSFT May 12 '17 at 02:09