简体   繁体   English

任务取消了长时间运行的任务的例外

[英]Task canceled exception for long running task

I have a method in a WPF client for a Web API: 我在WPF客户端中有用于Web API的方法:

public async Task PostFileAsync(string localPath, string serverPath)

The meat of this method is the call: 该方法的关键是调用:

var resp = await _client.PostAsync(uri, content);

where _client is an HttpClient instance. 其中_clientHttpClient实例。

When I try uploading large files that apparently take too long, I get an A task was canceled. 当我尝试上传似乎花费太长时间的大文件时,我得到了A task was canceled. exception, with the following stack trace: 异常,具有以下堆栈跟踪:

at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)
at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
at System.Net.Http.HttpClient.<FinishSendAsyncBuffered>d__58.MoveNext()
--- End of stack trace from previous location where exception was thrown ---
at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)
at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
at System.Runtime.CompilerServices.TaskAwaiter`1.GetResult()
at QuickDrive.Wpf.Api.ApiClient.<PostFileAsync>d__10.MoveNext() in C:\Projects\QuickDrive\Code\QuickDrive.Wpf\Api\ApiClient.cs:line 176

I see no mention of any timeout here, but this task completes nicely for posts that take less than about 10 minutes (I haven't timed it exactly), eg on my connection for files smaller than +-500MB. 我在这里没有提到任何超时,但是对于少于大约10分钟的帖子(我尚未准确计时),此任务可以很好地完成,例如,对于小于+ -500MB的文件,在我的连接上。

Is this exception really because of a timeout, and if so, how can I configure the await to allow for a configurable time before canceling the task; 这个异常是否真的是由于超时引起的,如果是这样,那么在取消任务之前,如何配置await以允许可配置的时间? assuming the task is canceled because of a timeout. 假设由于超时而取消了任务。 It never gets canceled for any other scenario than when I try and upload a large file. 除了尝试上传大型文件外,在任何其他情况下它都不会被取消。

Give this a try 试试这个

httpClient.Timeout httpClient.Timeout

Gets or sets the timespan to wait before the request times out. 获取或设置请求超时之前要等待的时间跨度。

To set an infinite timeout, set the property value to InfiniteTimeSpan. 要设置无限超时,请将属性值设置为InfiniteTimeSpan。

The HttpClient is just seemingly timing out and canceling the task HttpClient似乎只是在超时并取消任务

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM