user989988
user989988

Reputation: 3736

Retry paging Microsoft Graph data

I'm trying to get users from Microsoft Graph with the following code (followed this doc - https://learn.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();
        }

I'm trying to understand WithMaxRetry() property. Does that retry the call 5 times if the call fails? Is there a way to specify 'wait a few seconds before retrying each time'?

Upvotes: 0

Views: 957

Answers (1)

user2250152
user2250152

Reputation: 20605

Yes, it means that the call will retry 5 times if it fails.

The default delay before retrying a request is 3 seconds. You can create own extension method to be able to set the both maxRetry and 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;
    }
}

The maximum value for the delay is 180 s.

The extension method is based on BaseRequestExtensions

Upvotes: 1

Related Questions