本文介绍了HttpClient的 - 一个任务被取消?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
有一个或两个任务时,却抛出任务被取消的时候,我们有一个以上的任务中列出的错误它工作正常。
清单<任务> allTasks =新的List<任务>();
allTasks.Add(....);
allTasks.Add(....);
Task.WaitAll(allTasks.ToArray(),configuration.CancellationToken);
私人静态任务< T> HttpClientSendAsync< T>(字符串URL,对象数据,列举HTTPMethod方法,字符串的contentType,令牌的CancellationToken)
{
HttpRequestMessage httpRequestMessage =新HttpRequestMessage(方法,URL);
的HttpClient HttpClient的=新的HttpClient();
httpClient.Timeout =新的时间跨度(Constants.TimeOut);
如果(数据!= NULL)
{
字节[]的字节数组= Encoding.ASCII.GetBytes(Helper.ToJSON(数据));
的MemoryStream MemoryStream的=新的MemoryStream(字节阵列);
httpRequestMessage.Content =新的StringContent(新的StreamReader(的MemoryStream).ReadToEnd(),Encoding.UTF8,则contentType);
}
返回httpClient.SendAsync(httpRequestMessage).ContinueWith(任务=>
{
VAR响应= task.Result;
返回响应。 。Content.ReadAsStringAsync()ContinueWith(stringTask =>
{
VAR JSON = stringTask.Result;
返回Helper.FromJSON< T>(JSON);
});
})展开();
}
解决方案
有2可能的原因,一个 TaskCanceledException
将被抛出:
- 一种叫
取消( )
在CancellationTokenSource
与完成任务之前取消令牌关联。 - 请求超时,即没有你
HttpClient.Timeout
指定的时间跨度内完成。
我的猜测是,这是一个超时。 (如果这是明确的取消,你可能会想通了这一点。)你可以更加确定通过检查异常:
试
{
VAR响应= task.Result;
}
赶上(TaskCanceledException前)
{
//这里检查ex.CancellationToken.IsCancellationRequested。
//如果是假,这是相当安全的假设这是一个超时。
}
It works fine when have one or two tasks however throws an error "A task was cancelled" when we have more than one task listed.
List<Task> allTasks = new List<Task>();
allTasks.Add(....);
allTasks.Add(....);
Task.WaitAll(allTasks.ToArray(), configuration.CancellationToken);
private static Task<T> HttpClientSendAsync<T>(string url, object data, HttpMethod method, string contentType, CancellationToken token)
{
HttpRequestMessage httpRequestMessage = new HttpRequestMessage(method, url);
HttpClient httpClient = new HttpClient();
httpClient.Timeout = new TimeSpan(Constants.TimeOut);
if (data != null)
{
byte[] byteArray = Encoding.ASCII.GetBytes(Helper.ToJSON(data));
MemoryStream memoryStream = new MemoryStream(byteArray);
httpRequestMessage.Content = new StringContent(new StreamReader(memoryStream).ReadToEnd(), Encoding.UTF8, contentType);
}
return httpClient.SendAsync(httpRequestMessage).ContinueWith(task =>
{
var response = task.Result;
return response.Content.ReadAsStringAsync().ContinueWith(stringTask =>
{
var json = stringTask.Result;
return Helper.FromJSON<T>(json);
});
}).Unwrap();
}
解决方案
There's 2 likely reasons that a TaskCanceledException
would be thrown:
- Something called
Cancel()
on theCancellationTokenSource
associated with the cancellation token before the task completed. - The request timed out, i.e. didn't complete within the timespan you specified on
HttpClient.Timeout
.
My guess is it was a timeout. (If it was an explicit cancellation, you probably would have figured that out.) You can be more certain by inspecting the exception:
try
{
var response = task.Result;
}
catch (TaskCanceledException ex)
{
// Check ex.CancellationToken.IsCancellationRequested here.
// If false, it's pretty safe to assume it was a timeout.
}
这篇关于HttpClient的 - 一个任务被取消?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!