Retry-Policy -> The request message was already sent.
We are developing a Blazor (client sided) Frontend. Inside this Frontend we want to use a generated OpenAPI-Client to ease the access to our Backend.
These are the options we use to generate the client: Version 5.1.0 csharp-netcore target-framework net5.0 library httpclient
Works fine.
But, we also want to implement a retry-policy. Something like that:
RetryConfiguration.AsyncRetryPolicy = Policy<HttpResponseMessage>.HandleResult(rm => rm.StatusCode == System.Net.HttpStatusCode.Unauthorized).RetryAsync(1, onRetryAsync: async (response, retryCount, context)=>
{
DoSomething()
});
Problem is:
- first request is executed and returns 401as expected
- DoSomething() is called
- Retry is not possible -> The request message was already sent.
Our guess is that the error is made here (generated ApiClient.cs), and that client.SendAsync is reusing the request which is not possible.
if (RetryConfiguration.AsyncRetryPolicy != null)
{
var policy = RetryConfiguration.AsyncRetryPolicy;
var policyResult = await policy
.ExecuteAndCaptureAsync(() => client.SendAsync(req, cancellationToken))
.ConfigureAwait(false);
response = (policyResult.Outcome == OutcomeType.Successful) ?
policyResult.Result : new HttpResponseMessage()
{
ReasonPhrase = policyResult.FinalException.ToString(),
RequestMessage = req
};
}
else
{
response = await client.SendAsync(req, cancellationToken).ConfigureAwait(false);
}
This issue does not appear when using restsharp library instead of httpclient.
When using httpclient, I reproduced this also on a newer version:
Version 7.6.0
csharp
target-framework net8.0