2

In version 4 of the Microsoft Graph SDK (dotnet) you had the ability to use the fluent methods .WithShouldRetry and .WithMaxRetry. See in example this blogpost.

I'm now upgrading my application to v5 of the SDK and because of the removal of .Request() it's not possible to set the retry anymore.

How can we implement the retry in SDK v5?

Update: See new blogpost for v5 of the SDK.

2

2 Answers 2

1

The retryhandler is now a IRequestOption in Microsoft.Kiota.Http.HttpClientLibrary.Middleware.Options so you can do something like

    using Microsoft.Kiota.Http.HttpClientLibrary.Middleware.Options;

    private static Event CreateEvent(GraphServiceClient graphServiceClient, string emailAddress)
    {
        var requestBody = new Event
        {
            Subject = "Let's go for lunch",
            Body = new ItemBody
            {
                ContentType = BodyType.Html,
                Content = "Does mid month work for you?",
            },
            Start = new DateTimeTimeZone
            {
                DateTime = "2023-03-15T12:00:00",
                TimeZone = "Pacific Standard Time",
            },
            End = new DateTimeTimeZone
            {
                DateTime = "2023-03-15T14:00:00",
                TimeZone = "Pacific Standard Time",
            },
            Location = new Location
            {
                DisplayName = "Harry's Bar",
            },
            TransactionId = "7E163156-7762-4BEB-A1C6-729EA81755A7",
        };
        RetryHandlerOption retryHandlerOption = new RetryHandlerOption()
        {
            MaxRetry = 5
        };           
        var requestOptions = new List<IRequestOption>
        {                
            retryHandlerOption,
        };
        return graphServiceClient.Users[emailAddress].Calendar.Events.PostAsync(requestBody, rc => rc.Options = requestOptions).GetAwaiter().GetResult();
    }
1
  • Thank you! This was exactly what I was looking for. Will update my original blogpost and see my own answer.
    – LockTar
    Mar 13 at 10:57
1

@Glen Scales pointed me in the right direction. Here a more simple example with custom retry functionality.

// using Microsoft.Kiota.Abstractions;
// using Microsoft.Kiota.Http.HttpClientLibrary.Middleware.Options;

// Use random Guid for user that doesn't exist to force retry
string userId = Guid.NewGuid().ToString();

const int MaxRetry = 5; // So number of call are MaxRetry + 1 (1 is the original call)

RetryHandlerOption retryHandlerOption = new RetryHandlerOption()
{
    MaxRetry = MaxRetry,
    ShouldRetry = (delay, attempt, httpResponse) =>
    {
        Console.WriteLine($"Request returned status code {httpResponse.StatusCode}");

        // Add more status codes here or change your if statement...
        if (httpResponse.StatusCode == System.Net.HttpStatusCode.Unauthorized)
            return false;

        double delayInSeconds = CalculateDelay(httpResponse, attempt, delay);

        if (attempt == 0)
            Console.WriteLine($"Request failed, let's retry after a delay of {delayInSeconds} seconds");
        else if (attempt == MaxRetry)
            Console.WriteLine($"This was the last retry attempt {attempt}");
        else
            Console.WriteLine($"This was retry attempt {attempt}, let's retry after a delay of {delayInSeconds} seconds");

        return true;
    }
};

var requestOptions = new List<IRequestOption>
{
    retryHandlerOption,
};

User? user = await graphClient
    .Users[userId]
    .GetAsync(requestConfiguration => requestConfiguration.Options = requestOptions);
1

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.