0

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), e.g. 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.

halfer
  • 19,824
  • 17
  • 99
  • 186
ProfK
  • 49,207
  • 121
  • 399
  • 775
  • 2
    This has nothing to do with the `Task` class, try setting the `HttpClient.Timeout` property. – Zein Makki Jun 11 '18 at 09:47
  • This is why cloud storage solutions offer the option of uploading distinct chunks of data to an endpoint, then threading them together with a manifest of the uploaded chunks. Weighty uploads are perilous in many different ways. Consider an approach that doesn't leave you butting up against any pre-configured timeouts. – spender Jun 11 '18 at 09:51
  • @spender I've set a 1GB upload limit, so I don't think any upload should be so weighty as to be a real problem if I configure a fair timeout. We need to roll out asap so I would rather not introduce the complexities of chunked uploads at this stage, but thanks. I will consider improving the upload on a next version. – ProfK Jun 11 '18 at 11:09

1 Answers1

1

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

TheGeneral
  • 79,002
  • 9
  • 103
  • 141
  • Just a caveat: apparently you need to set the timeout on the stream istelf if you are using e.g. `response.content.ReadAsStreamAsync` anywhere, as the stream has its own timeout. Refer to this answer: https://stackoverflow.com/a/45667609/8741 – ProfK Jun 14 '18 at 09:11