简体   繁体   English

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

[英]Unable to get queue length / message count from Azure

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.由于我在 Azure 中运行,我正在尝试使用RetrieveApproximateMessageCount()方法来获取当前消息计数。 Everytime I call this I get an exception stating StorageClientException: The specified queue does not exist.每次我调用它时,我都会收到一个异常,说明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在门户中创建了存储帐户,它处于 Created/Online 状态

  3. Coded the query as follows ( using http and https options ):将查询编码如下(使用 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;
  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 ( eg http://myaccount.queue.core.windows.net/myqueue )我已经确认名称大小写正确,没有特殊字符、数字或空格,并且生成的queue Url 看起来好像是根据 API 文档(例如http://myaccount.queue.core.windows.net/我的队列

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.我已经确认使用MessageFactory我可以创建一个QueueClient ,然后成功地使消息入队/出队。 When I use the CloudStorageAccount the queue is never present so the counts and GetMessage routines never work.当我使用CloudStorageAccount时,队列永远不会出现,因此计数和 GetMessage 例程永远不会工作。 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 RetrieveApproximateMessageCount()已被弃用

if you want to use ApproximateMessageCount to get result try this如果你想使用 ApproximateMessageCount 来获得结果试试这个

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

The CloudQueue method has been deprecated (along with the v11 SDK). CloudQueue 方法已被弃用(连同 v11 SDK)。

The following snippet is the current replacement (from the Azure Docs )以下片段是当前的替代品(来自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 https://learn.microsoft.com/en-us/azure/storage/queues/storage-dotnet-how-to-use-queues?tabs=dotnet#get-the-queue-length

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 使用 Microsoft.Azure.Management.ServiceBus 获取队列消息计数 - Get queue message count using Microsoft.Azure.Management.ServiceBus 从 .net 5 中的 Azure 服务总线队列获取消息 ID - Get message id from Azure Service Bus Queue in .net 5 从WPF应用程序向Azure队列发送消息 - Sending message to Azure queue from WPF application 检查消息是否从 azure 队列中删除 - Check if message is deleted from azure queue 无法使用Azure功能将消息发送到Service Bus队列 - Unable to send message into Service Bus Queue using Azure function 如何从本地Win7计算机上的专用队列获取MSMQ消息计数? - How do I get MSMQ message count from a private queue on local Win7 machine? 为什么在队列中添加队列消息时,Azure webjob 无法执行 ProcessQueueMessage? - Why Azure webjob unable to execute ProcessQueueMessage when queue message is added in queue? Azure服务总线/服务结构消息未从队列中删除 - Azure Service Bus/Service Fabric message not being removed from queue Windows 服务使用队列中的 Azure 服务总线消息 - Windows services to consume Azure service bus message from queue Azure函数从服务总线两次读取队列消息 - Azure function reading queue message twice from service bus
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM