[英]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:
这是对我所做工作的回顾:
Created the queue in the portal and have successfully queued messages to it.在门户中创建队列并已成功将消息排队。
Created the storage account in the portal and it is in the Created/Online state在门户中创建了存储帐户,它处于 Created/Online 状态
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;
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.