I'm trying to implement retry using DelegatingHandler but I also want to set HttpCompletionOption.
My code looks like following :
public class RetryHandler : DelegatingHandler
{
private readonly int MaxRetries;
public RetryHandler(HttpMessageHandler innerHandler, int retryCount = 3)
: base(innerHandler)
{
MaxRetries = retryCount;
}
protected override async Task<HttpResponseMessage> SendAsync(
HttpRequestMessage request,
CancellationToken cancellationToken)
{
HttpResponseMessage response = null;
for (var i = 0; i < MaxRetries; i++)
{
response = await base.SendAsync(request, cancellationToken);
if (response.IsSuccessStatusCode)
{
return response;
}
}
return response;
}
}
static void Main(string[] args)
{
using (var client = new HttpClient(new RetryHandler(new HttpClientHandler())))
{
var request = new HttpRequestMessage(HttpMethod.Post, "https://ptsv2.com/t/smz9v-1564979884/post");
var myResult = client.SendAsync(request).Result;
Console.WriteLine(myResult.ToString());
}
Console.ReadLine();
}
As you can see there is no option to set HttpCompletionOption by inheriting DelegatingHandler, I tried creating CustomDelegatinghandler but in that case I'm unable to use new RetryHandler(new HttpClientHandler()) as I have used in the main method.
Is there any way to implement DelegatingHandler(or custom) that has/ supports following signature?
protected override async Task<HttpResponseMessage> SendAsync(
HttpRequestMessage request, HttpCompletionOption option,
CancellationToken cancellationToken)
Any help would be appreciated. Thanks,
You don't need to include
HttpCompletionOptionin the message handler signature, because handlingHttpCompletionOptionis responsibility ofHttpClient, not ofHttpMessageHandler.An
HttpMessageHandlermust initiate a task of sending a request and return aTask<HttpResponseMessage>as early as possible. In general, it should not perform any buffering of the response content (well, unless it's a middleware created for the purpose of buffering), and that's why it doesn't receiveHttpCompletionOption. It should always act as if it was passedHttpCompletionOption.ResponseHeadersRead.The intended way to specify
HttpCompletionOptionis include it in a call to an overload ofHttpClientmethod such asGetAsyncorSendAsync:Roughly speaking,
HttpClienthandlesHttpCompletionOptionas follows:Task<HttpResponseMessage>(see code)HttpCompletionOption.ResponseHeadersReadis specified, then done: return the task to the caller (see code)HttpCompletionOption.ResponseContentReadis specified, then instead, return the task ofHttpContent.LoadIntoBufferAsync, as a continuation of the originalTask<HttpResponseMessage>(see code)