无法从Azure获取队列长度/消息计数

Joe*_*eky 8 c# azure azure-storage azure-queues

我有一个用例,当前队列长度低于指定值时,我需要对选定数量的消息进行排队.由于我在Azure中运行,我正在尝试使用该RetrieveApproximateMessageCount()方法来获取当前的消息计数.每次我打电话给我都会得到一个例外陈述StorageClientException: The specified queue does not exist..以下是对我所做的事情的回顾:

  1. 在门户中创建了队列,并已成功将消息排入其中.

  2. 在门户中创建存储帐户,它处于创建/联机状态

  3. 编码查询如下(使用http和https选项):

    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;
    
    Run Code Online (Sandbox Code Playgroud)
  4. 我已经确认名称是正确的,没有特殊字符,数字或空格,并且生成的queueUrl看起来好像是基于API文档正确形成的(例如http://myaccount.queue.core.windows.net/myqueue)

任何人都可以帮助阐明我做错了什么.


编辑

我已经确认使用MessageFactory我可以创建一个QueueClient然后成功排队/出列消息.当我使用CloudStorageAccount队列时从不存在,因此计数和GetMessage例程永远不会工作.我猜这些不是一回事??? 假设,我是对的,我需要的是测量服务总线队列的长度.那可能吗?

Nag*_*esh 51

RetrieveApproximateMessageCount()已被弃用

如果你想使用ApproximateMessageCount来获得结果,试试这个

CloudQueue q = queueClient.GetQueueReference(QUEUE_NAME);
q.FetchAttributes();
qCnt = q.ApproximateMessageCount;
Run Code Online (Sandbox Code Playgroud)

  • 如果 `ApproximateMessageCount` 抛出一个令人愉快的描述性异常告诉你必须首先获取属性,那就太好了。 (2认同)
  • `CloudQueue` 现在也已被弃用(仅在旧版 SDK 中),因此现在已经过时了 (2认同)

nul*_*rce 9

CloudQueue 方法已被弃用(与 v11 SDK 一起)。

以下代码片段是当前的替换内容(来自Azure 文档

//-----------------------------------------------------
// 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}");
    }
}
Run Code Online (Sandbox Code Playgroud)

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