我正在尝试从 Azure 服务获取所有内容,它返回 AsyncPageable。根据文档它说
可能需要多个服务请求进行迭代的值的集合。
这是否意味着它等于通过循环多次请求单个项目?
Har*_*ngh 18
如果服务调用在页面中返回多个值,则会返回Pageable<T>/AsyncPageable<T>作为结果。查看返回 AsyncPageable 的消费服务方法。
为了更清楚地了解,请看下面:这显示了对从服务使用 AsyncPageable<T>.AsPages方法接收值页面的控制:
// call a service method, which returns AsyncPageable<T>
AsyncPageable<SecretProperties> response = client.GetPropertiesOfSecretsAsync();
await foreach (Page<SecretProperties> page in response.AsPages())
{
// enumerate through page items
foreach (SecretProperties secretProperties in page.Values)
{
Console.WriteLine(secretProperties.Name);
}
// get continuation token that can be used in AsPages call to resume enumeration
Console.WriteLine(page.ContinuationToken);
}
Run Code Online (Sandbox Code Playgroud)
如果您的项目没有启用 C# 8.0,您仍然可以使用while循环迭代 AsyncPageable:
// call a service method, which returns AsyncPageable<T>
AsyncPageable<SecretProperties> response = client.GetPropertiesOfSecretsAsync();
IAsyncEnumerator<SecretProperties> enumerator = response.GetAsyncEnumerator();
try
{
while (await enumerator.MoveNextAsync())
{
SecretProperties secretProperties = enumerator.Current;
Console.WriteLine(secretProperties.Name);
}
}
finally
{
await enumerator.DisposeAsync();
}
Run Code Online (Sandbox Code Playgroud)
查看Azure.Core 响应示例以了解更多相关信息。
要更改页面大小,您可以使用方法pageSizeHint参数AsPages。
| 归档时间: |
|
| 查看次数: |
11068 次 |
| 最近记录: |