I'm writing a console application that uploads large files to dropbox. I want my program to resume failed upload whenever it accidentally exits or drops connection. I retrive the uploadID and offset from a txt file to achieve this.
But I constantly get "A task was canceled" issue. And I figured it is a timeout error. I have set "Timeout = TimeSpan.FromMinutes(30)" but it isn't working.
So how can I fix this?
Below is my code:
try
{
var httpClient = new HttpClient(new WebRequestHandler { ReadWriteTimeout = 10 * 1000 })
{
// Specify request level timeout which decides maximum time that can be spent on
// download/upload files.
Timeout = TimeSpan.FromMinutes(30)
};
var response = await _client.Core.Metadata.ChunkedUploadAsync(fragmentBuffer, count, uploadId, currentPosition, asTeamMember, cancellationToken).ConfigureAwait(false);
Console.Write("\r\n2try: {0} of {1} uploaded. uploadId: {2}\r\n", currentPosition, _dataSource.Length, uploadId);
uploadId = response.upload_id;
var testoffset = response.offset;
uploadoffset = testoffset;
var testexpires = response.expires;
currentPosition = endPosition;
Console.Write("\r\n3currentPosition: {0} endPosition: {1} testexpires: {2} count: {3} \r\n", currentPosition, endPosition, testexpires, count);
}
//catch (TaskCanceledException ex)
//{
// // Check ex.CancellationToken.IsCancellationRequested here.
// // If false, it's pretty safe to assume it was a timeout.
// Console.WriteLine("\r\nerr TaskCanceledException: {0}\r\n", ex.CancellationToken.ToString());
//}
catch (Exception e)
{
//write down offset and uploadID to a txt
StreamWriter sw = new StreamWriter("1.txt");
sw.WriteLine(uploadoffset.ToString());
sw.WriteLine(uploadId.ToString());
sw.Close();
Console.WriteLine("7Err getting response:{0} .. Make sure you have connection again then press enter to resume. \n", e.Message);
Console.Write("7catch: currentPosition: {0} endPosition: {1} \r\n", currentPosition, endPosition);
Console.ReadLine();
}