i am trying to overcome intermittent 409 error that occurs while uploading/updating metadata of a file in SharePoint's Document library using Microsoft Graph SDK. To retry failed calls SDK provides WithMaxRetry() and WithShouldRetry() options. The MaxRetry works for error codes 429, and i am assuming that ShouldRetry delegate offers us an option to implement our own logic for the retries. Based on this assumption, i have the below code:
_graphServiceClientFactory.GetClient().Drives[driveId].Root.ItemWithPath(path).ListItem.Fields.Request()
.WithShouldRetry((delay, attempt, httpResponse) =>
(attempt <= 5 &&
(httpResponse.StatusCode == HttpStatusCode.Conflict)))
.UpdateAsync(new FieldValueSet { AdditionalData = dataDictionary });
In my test the ShouldRetry delegate was never called/evaluated on failures/otherwise, there is no documentation on the usage of WithShouldRetry(). It would be helpful to get inputs on usage of WithShouldRetry() option.
It appears that the WithShouldRetry() is faulty, i had reported this issue in GitHub (Microsft Graph SDK repo), and they have marked the issue as Bug.
As a workaround one could use Polly for retry as shown below,
By default Graph SDK does 3 retries for throttled and gateway timeout errors. In the above code those native retries have been blocked by calling WithMaxRetry(0). The internal retry logic of Graph SDK is made part of the Polly implementation.
Note: This Polly implementation should be a temporary solution, i believe it is best to utilize the WithShouldRetry() once the reported bug is resolved.