Вопрос

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?

Это было полезно?

Решение

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;

Другие советы

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://docs.microsoft.com/en-us/azure/storage/queues/storage-dotnet-how-to-use-queues?tabs=dotnet#get-the-queue-length

Лицензировано под: CC-BY-SA с атрибуция
Не связан с StackOverflow
scroll top