【发布时间】:2016-09-23 17:18:40
【问题描述】:
我看到很多参考文献提到 CloudQueueClient 类中的“结果段”一词,例如在“ListQueuesSegmented”和“ListQueuesSegmentedAsync”方法中。但是我没有找到任何有意义的例子来说明如何使用这些功能。任何 Azure 专家都可以解释一下吗?
谢谢
德里克
【问题讨论】:
标签: azure azure-webjobs azure-webjobssdk
我看到很多参考文献提到 CloudQueueClient 类中的“结果段”一词,例如在“ListQueuesSegmented”和“ListQueuesSegmentedAsync”方法中。但是我没有找到任何有意义的例子来说明如何使用这些功能。任何 Azure 专家都可以解释一下吗?
谢谢
德里克
【问题讨论】:
标签: azure azure-webjobs azure-webjobssdk
创建存储帐户时,您可以在其中创建无限数量的队列(有关详细信息,请参阅storage limits)。
假设您想获取有关所有队列的信息,您可以使用CloudQueueClient.ListQueues 方法遍历所有队列:
var storageAccount = CloudStorageAccount.Parse("MyConnectionString");
var queueClient = storageAccount.CreateCloudQueueClient();
foreach(var queue in queueClient.ListQueues())
{
// Do something
}
假设你有一千个队列,你可能不想执行这个请求,因为它可能会超时,达到一些限制。
Segmented 方法的所有目的。它将返回第一个 X 元素 + 一个允许您请求下一个 X 元素的令牌。
当您使用表格(在 UI 端)显示数据时,有时您必须使用分页,因为您的表格可能太大而无法完全显示:这是相同的概念。
所以现在如果你想使用它:
// Initialize a new token
var continuationToken = new QueueContinuationToken();
// Execute the query
var segment = queueClient.ListQueuesSegmented(continuationToken);
// Get the new token in order to get the next segment
continuationToken = segment.ContinuationToken;
// Get the results
var queues = segment.Results.ToList();
// do something
...
// Execute the query again with the comtinuation token to fetch next results
segment = queueClient.ListQueuesSegmented(continuationToken);
【讨论】:
在https://github.com/Azure-Samples/storage-queue-dotnet-getting-started/blob/master/QueueStorage/Advanced.cs有如何使用这些函数的示例
关于如何通过 ListQueuesSegmentedAsync 进行分页的具体方法是:(注意初始化为空令牌似乎是正确的,检测空令牌结束链也是如此)
Console.WriteLine(string.Empty);
Console.WriteLine("List of queues in the storage account:");
// List the queues for this storage account
QueueContinuationToken token = null;
List<CloudQueue> cloudQueueList = new List<CloudQueue>();
do
{
QueueResultSegment segment = await cloudQueueClient.ListQueuesSegmentedAsync(token);
token = segment.ContinuationToken;
cloudQueueList.AddRange(segment.Results);
}
while (token != null);
【讨论】: