【问题标题】:Retry paging Microsoft Graph data重试分页 Microsoft Graph 数据
【发布时间】:2021-06-24 06:24:38
【问题描述】:
我正在尝试使用以下代码从 Microsoft Graph 获取用户(关注此文档 - https://docs.microsoft.com/en-us/graph/paging):
private readonly IGraphServiceClient _graphServiceClient;
public async Task<IGroupTransitiveMembersCollectionWithReferencesPage> GetGroupMembersPageByIdAsync(string groupId)
{
return await _graphServiceClient
.Groups[groupId]
.TransitiveMembers
.Request()
.Top(999)
.WithMaxRetry(5)
.GetAsync();
}
我正在尝试了解 WithMaxRetry() 属性。如果呼叫失败,是否会重试呼叫 5 次?有没有办法指定“每次重试前等待几秒钟”?
【问题讨论】:
标签:
microsoft-graph-api
graphserviceclient
【解决方案1】:
是的,就是调用失败会重试5次。
重试请求前的默认延迟为 3 秒。您可以创建自己的扩展方法来设置maxRetry 和delay。
public static class Helper
{
/// <summary>
/// Sets the maximum number of retries to the default Retry Middleware Handler for this request.
/// This only works with the default Retry Middleware Handler.
/// If you use a custom Retry Middleware Handler, you have to handle it's retrieval in your implementation.
/// </summary>
/// <typeparam name="T"></typeparam>
/// <param name="baseRequest">The <see cref="BaseRequest"/> for the request.</param>
/// <param name="maxRetry">The maxRetry for the request.</param>
/// <param name="delay">The delay before the request.</param>
/// <returns></returns>
public static T WithMaxRetry<T>(this T baseRequest, int maxRetry, int delay) where T : IBaseRequest
{
string retryOptionKey = typeof(RetryHandlerOption).ToString();
if (baseRequest.MiddlewareOptions.ContainsKey(retryOptionKey))
{
var option = (baseRequest.MiddlewareOptions[retryOptionKey] as RetryHandlerOption);
option.MaxRetry = maxRetry;
option.Delay = delay;
}
else
{
baseRequest.MiddlewareOptions.Add(retryOptionKey, new RetryHandlerOption { MaxRetry = maxRetry, Delay = delay });
}
return baseRequest;
}
}
延迟的最大值为 180 秒。
扩展方法基于BaseRequestExtensions