You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

.NET Azure SDK如何获取异步分页结果的原始HTTP响应?

分页场景下获取Azure.ResourceManager SDK的原始HTTP响应

你当前的代码可以正常列出订阅中的所有资源,但要在分页场景下获取原始HTTP响应,需要直接操作AsyncPageable<T>的分页枚举器——每个分页结果(Page<T>)都包含对应的原始响应对象。

修改后的代码示例:

var armClient = new ArmClient(new DefaultAzureCredential());
var subscription = await armClient.GetDefaultSubscriptionAsync();

// 获取分页可枚举对象
var resourcePages = subscription.GetGenericResourcesAsync();
await using var pageEnumerator = resourcePages.GetAsyncEnumerator();

while (await pageEnumerator.MoveNextAsync())
{
    // 获取当前分页的原始HTTP响应
    var rawResponse = pageEnumerator.Current.GetRawResponse();
    _logger.LogInformation("当前页响应状态码: {StatusCode}", rawResponse.Status);
    _logger.LogInformation("当前页响应头: {Headers}", rawResponse.Headers);

    // 遍历当前页的资源
    foreach (var resource in pageEnumerator.Current)
    {
        _logger.LogInformation("Got resource {id}", resource.Id);
    }
}

说明

  • AsyncPageable<T>的GetAsyncEnumerator()方法返回异步枚举器,其Current属性是Page<T>类型
  • Page<T>自带GetRawResponse()方法,可直接获取该分页对应的原始HTTP响应,包含状态码、响应头、响应体等信息
  • 使用await using确保异步枚举器被正确释放

内容的提问来源于stack exchange,提问作者silent

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.11 03:05:27