问题描述
在.NET中为RetryHandler
使用自定义HttpClient
。
//Create client
var client = new HttpClient(new RetryHandler(new HttpClientHandler()));
//PutAsync
var result = await client.PutAsync(new Uri($"{fileSystem}?resource=filesystem"),null).ConfigureAwait(true);
//Retry Handler
public class RetryHandler : DelegatingHandler
{
private const int MaxRetry = 3;
private const int DelayMilliSeconds = 10000;
public RetryHandler(HttpMessageHandler innerHandler)
: base(innerHandler)
{
}
protected override async Task<HttpResponseMessage> SendAsync(
HttpRequestMessage request,CancellationToken cancellationToken)
{
HttpResponseMessage response = null;
for (int i = 0; i < MaxRetry; i++)
{
try
{
CancellationTokenSource source = new CancellationTokenSource(TimeSpan.FromMinutes(5));
response = await base.SendAsync(request,source.Token).ConfigureAwait(true);
if (response.IsSuccessStatusCode)
{
return response;
}
}
catch (Exception e)
{
Thread.Sleep(TimeSpan.FromMilliseconds(Math.Pow(2,i) * DelayMilliSeconds));
continue;
}
Thread.Sleep(TimeSpan.FromMilliseconds(Math.Pow(2,i) * DelayMilliSeconds));
}
return response;
}
看起来它从未输入RetryHandler
,在输入RetryHandler
之前得到了以下错误。
System.Net.Http.HttpRequestException:将内容复制到流时出错。 ---> System.ObjectDisposedException:无法访问关闭的Stream。 在System.IO.Stream.CopyToAsync(流目标,Int32 bufferSize,CancellationToken cancelToken) 在System.Net.Http.DelegatingStream.CopyToAsync(流目标,Int32缓冲区大小,CancellationToken取消令牌) 在System.Net.Http.StreamToStreamCopy.CopyAsync(流源,流目标,Int32 bufferSize,布尔值disposeSource,CancellationToken cancelleToken)
解决方法
暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!
如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。
小编邮箱:dio#foxmail.com (将#修改为@)