简体   繁体   中英

Task canceled exception for long running task

I have a method in a WPF client for a 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.

When I try uploading large files that apparently take too long, I get an 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.

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; 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

Gets or sets the timespan to wait before the request times out.

To set an infinite timeout, set the property value to InfiniteTimeSpan.

The HttpClient is just seemingly timing out and canceling the task

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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