Search code examples
c#.net-coremicrosoft-graph-api

Where is WithMaxRetry and WithShouldRetry in dotnet graph SDK v5


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.


Solution

  • 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();
        }