locked
How to eliminate "A Task was canceled : TaskCanceledException" while using HttpClient ? RRS feed

  • Question

  • Hi, I want to do something complex with help of HttpClient. I am parsing nearly (264 * n) RSS feeds. Where "n" is number of object in the list, it can be 1 to n. So what I do is first I create n number of task with a method, and then another method contains all 264 tasks. 

    So at some time it throws TaskCanceledException at random time, so my all the tasks are not getting completed. So what's the problem with my code ?

    private static Dictionary<string, dbReviewCount> appsToReportOn = new Dictionary<string, dbReviewCount>(); private static List<RankingResult> rankingResults = new List<RankingResult>(); private static List<string> Countires = new List<string>(); public static async Task CollectRankingsForApp(List<dbReviewCount> appsWeCareAbout) { Countires = await LoadCountires(); //264 countries foreach (var app in appsWeCareAbout) { appsToReportOn.Add(app.Apple_Identifier, app); } await Task.WhenAll(appsToReportOn.Select(x => CollectRankingsForApp(x.Value))); foreach (var result in rankingResults) { System.Diagnostics.Debug.WriteLine(result); } } private static async Task CollectRankingsForApp(dbReviewCount appDetail) { await Task.WhenAll(Countires.Select(countryDetails => CollectRankingsForApp(appDetail, countryDetails))); } private static async Task CollectRankingsForApp(dbReviewCount appDetail, string countryDetails) { string requestResultString = string.Empty; // This is RSS url which takes two parameters, one is country and another

    //is appDetail hence total urls will be [264 * (appsWeCareAbout.Count)] string targetURL = "https://xxxxxx.blabla.com/......."; try { var httpClient = new HttpClient(); var message = new HttpRequestMessage(HttpMethod.Get, targetURL); message.Headers.Add("User-Agent", MyUserAgent); message.Headers.Add("Pragma", "no-cache"); var response = await httpClient.SendAsync(message); //TaskCanceledException occurs here if (response.StatusCode != HttpStatusCode.OK) { requestResultString = "ERROR!"; System.Diagnostics.Debug.WriteLine("Not availble App : {0} Country : {1}", appDetail.App_Name, countryDetails); } requestResultString = await response.Content.ReadAsStringAsync(); } catch (TaskCanceledException timeoutEx) { System.Diagnostics.Debug.WriteLine(timeoutEx); return; } catch (Exception ex) { System.Diagnostics.Debug.WriteLine(ex); return; } /*Parsing RSS with Json.Net*/ uint rank = 0; foreach (var entry in output["feed"]["entry"]) { try { string appId = ""; /*Getting appId from entry logic*/ var app = appsToReportOn[appId]; /*some more logic*/ lock (rankingResults) { // Add our result rankingResults.Add(new RankingResult() { RankingApp = app.App_Name, CountryDetail = countryDetails, ResultDate = DateTime.UtcNow, RankPosition = rank }); } } catch (Exception ex) { System.Diagnostics.Debug.WriteLine(ex); } rank++; } }



    • Edited by Xyroid Friday, June 14, 2013 1:47 PM
    Friday, June 14, 2013 1:45 PM

All replies

  • I believe HttpClient will raise a TaskCanceledException if the send times out. This isn't a problem with your code. It is just a scenario that your code will need to handle. One possibility is to wait a bit and then try again.

    --Rob

    Saturday, June 15, 2013 2:28 AM
    Moderator
  • I know default time out is of 100 seconds, so how can I handle that time out problem ?
    Saturday, June 15, 2013 5:35 AM
  • Hi Xyroid,

    I am facing similar problem while download large file, did you find any solution for that?

    Thanks in advance.

    Monday, June 9, 2014 1:11 PM
  • I know default time out is of 100 seconds, so how can I handle that time out problem ?

    One possibility is to wait a bit and then try again.
    Monday, June 9, 2014 4:47 PM
    Moderator
  • Hi Rob,

    Still I am facing that problem, Getting TaskCanceledException while downloading large file.

    As you suggested to wait a bit and then to try again, I am not getting it exactly.

    It will be helpful if you share some code here that explains it.

    Thanks in advance.

    Monday, July 14, 2014 1:41 PM
  • Use background downloading for large file. HttpClient is intended for HTTP web service request & response.
    Monday, July 14, 2014 1:48 PM
  • Hi Xyroid,

    Thanks for your reply,

    I tried backgrouddownloader but it didn't support  autoredirect. Do you know how to set autoredirect true for it.

    Thanks in advance.


    • Edited by zee_patel Tuesday, July 15, 2014 5:27 AM
    Tuesday, July 15, 2014 5:27 AM
  • I don't have any idea about such things. Try to use SetRequestHeader method if it helps you. You can also try first HttpClient to get the download URL first & then use background download.
    Tuesday, July 15, 2014 6:06 AM
  • Hi Xyroid

    Thanks for your suggestion. I tried that but it's not working. Here is the code.

    var downloader = new BackgroundDownloader();
    downloader.SetRequestHeader("Cookie", userSession.ToString());
    DownloadOperation download = downloader.CreateDownload(new Uri(fileDownloadUrl) , file);
    await StartDownloadAsync(download);
    Also tried the way described here,  but still not working.


    • Edited by zee_patel Tuesday, July 15, 2014 6:21 AM
    Tuesday, July 15, 2014 6:21 AM