16

I have a Use Case where I need to queue a select number of messages when the current queue length drops below a specified value. Since I'm running in Azure, I'm trying to use the RetrieveApproximateMessageCount() method to get the current message count. Everytime I call this I get an exception stating StorageClientException: The specified queue does not exist.. Here is a review of what I've done:

  1. Created the queue in the portal and have successfully queued messages to it.

  2. Created the storage account in the portal and it is in the Created/Online state

  3. Coded the query as follows (using http and https options):

    var storageAccount = new CloudStorageAccount(
            new StorageCredentialsAccountAndKey(_messagingConfiguration.StorageName.ToLower(),
            _messagingConfiguration.StorageKey), false);
    
    var queueClient = storageAccount.CreateCloudQueueClient();
    var queue = queueClient.GetQueueReference(queueName.ToLower());
    int messageCount;
    
    try
    {
        messageCount = queue.RetrieveApproximateMessageCount();
    }
    catch (Exception)
    {
        //Booom!!!!! in every case
    }
    
    // ApproximateMessageCount is always null
    
    messageCount = queue.ApproximateMessageCount == null ? 0 : queue.ApproximateMessageCount.Value;
    
  4. I've confirmed the name is cased correctly with not special characters, numbers, or spaces and the resulting queue Url appears as though its correct formed based on the API documentations (e.g. http://myaccount.queue.core.windows.net/myqueue)

Can anyone help shed some light on what I'm doing wrong.


EDIT

I've confirmed that using the MessageFactory I can create a QueueClient and then enqueue/dequeue messages successfully. When I use the CloudStorageAccount the queue is never present so the counts and GetMessage routines never work. I am guessing these are not the same thing??? Assuming, I'm correct, what I need is to measure the length of the Service Bus Queue. Is that possible?

JoeGeeky
  • 3,746
  • 6
  • 36
  • 53
  • And... does the queue exist? :-) What happens if you call `queue.AddMessage(...)` or `queue.GetMessage(...)`? – user94559 Jun 21 '12 at 00:20
  • @smarx I can queue and dequeue with no trouble. In my mind, that means it really exists. Nevermind the fact I created it in the portal, so it should exist. (Boggle!) – JoeGeeky Jun 21 '12 at 00:33
  • @smarx I should have mentioned that the enqueue and dequeue are done in seperate processes so it must exist. – JoeGeeky Jun 21 '12 at 00:34
  • 2
    You can't create a queue in the portal. I'm sticking with "The queue doesn't exist." Try putting `queue.GetMessage()` right before `queue.RetrieveApproximateMessageCount()`. I'm betting it fails with the same error. – user94559 Jun 21 '12 at 01:48
  • 1
    ..or try adding a 'CreateIfNotExists'. You might be connecting to a different storage account in the other process? – Richard Astbury Jun 21 '12 at 11:05
  • Try firing up fiddler and verifying the queue names used by both the enqueue/dequeue operations as well as the attempt to get the queque count. You can also try using a 3rd party storage utility to verify outside of your codee that the queue does exist and has contents. Some of these tools will support displaying the queue depth and thus can be used to verify that the operation is working. If the tools are displaying things properly, then there is something wrong in your code, either addressability or something not existing when you expect it too. – BrentDaCodeMonkey Jun 21 '12 at 12:57
  • When I go to `Services --> Service Bus --> Queues` in the portal I can see the Queue exists and has messages in it. When I use the `MessagingFactory` I can create a `QueueClient` and Enqueue/Dequeue with no problem, but can't see the length. When I use the `CloudStorageAccount` and generate a `CloudQueue` instance the queue does not exist. I'm guessing these are not the same queues? How can I get length of a Service Bus Queue? Have I just completely jumbled seperate tech? – JoeGeeky Jun 21 '12 at 14:05
  • 3
    You are using two different queuing technologies. It sounds like you've created a Service Bus Queue, but the code you're giving is for an Azure Storage Queue. They are not the same thing, despite having similar goals. – Brian Reischl Jun 21 '12 at 16:08
  • @breischl I finally came to understand that. Do you know if there is a way to get the Length of a Service Bus Queue? Then again, maybe I should withdraw this and post a different question. – JoeGeeky Jun 21 '12 at 16:14

2 Answers2

69

RetrieveApproximateMessageCount() has been deprecated

if you want to use ApproximateMessageCount to get result try this

CloudQueue q = queueClient.GetQueueReference(QUEUE_NAME);
q.FetchAttributes();
qCnt = q.ApproximateMessageCount;
Liam
  • 27,717
  • 28
  • 128
  • 190
Naga Sailesh
  • 786
  • 1
  • 8
  • 13
  • Such a simple answer for a simple problem. Nice. –  Mar 31 '16 at 09:00
  • 2
    Would be so nice if the `ApproximateMessageCount` threw a pleasant descriptive exception telling you to have to fetch the attributes first. – riezebosch May 28 '19 at 13:46
  • 2
    `CloudQueue` is now, also, deprecated (only in the legacy SDK) so this is out of date now – Liam Oct 27 '20 at 09:44
8

The CloudQueue method has been deprecated (along with the v11 SDK).

The following snippet is the current replacement (from the Azure Docs)

//-----------------------------------------------------
// Get the approximate number of messages in the queue
//-----------------------------------------------------
public void GetQueueLength(string queueName)
{
    // Get the connection string from app settings
    string connectionString = ConfigurationManager.AppSettings["StorageConnectionString"];

    // Instantiate a QueueClient which will be used to manipulate the queue
    QueueClient queueClient = new QueueClient(connectionString, queueName);

    if (queueClient.Exists())
    {
        QueueProperties properties = queueClient.GetProperties();

        // Retrieve the cached approximate message count.
        int cachedMessagesCount = properties.ApproximateMessagesCount;

        // Display number of messages.
        Console.WriteLine($"Number of messages in queue: {cachedMessagesCount}");
    }
}

https://learn.microsoft.com/en-us/azure/storage/queues/storage-dotnet-how-to-use-queues?tabs=dotnet#get-the-queue-length

nullforce
  • 1,051
  • 1
  • 8
  • 13