locked
TaskCancellationException with timeout parameter in Windows.Web.Http.HttpClient

    Question

  • I'm building a Windows Phone with Http requests and for that I'm using the Windows.Web.Http.HttpClient. I wanted to set the request timeout and found a suggestion to use a CancellationToken like the following code describes:

    HttpClient client = new HttpClient();
    CancellationTokenSource source = new CancellationTokenSource(2000);
    try {
    	client.GetAsync(new Uri("http://example.com")).AsTask(source.Token);
    } catch(TaskCanceledException ex) {
    	// Catch exception here
    }

    The problem is that after a few requests the TaskCanceledException is thrown and does so every request after the first exception.

    How can I set the a request to an HttpClient that acts like the System.Net.Http.HttpClient.Timeout but with the Windows.Web.Http.HttpClient class?

    Wednesday, September 24, 2014 10:04 AM

Answers

  • Have you try setting one CancellationTokenSource per request? If you use one, the token will expire after two seconds.
    Wednesday, September 24, 2014 3:36 PM

All replies

  • I think the networking Pm would be interested in seeing this... I'll ask him to check this out.

    Matt Small - Microsoft Escalation Engineer - Forum Moderator
    If my reply answers your question, please mark this post as answered.

    NOTE: If I ask for code, please provide something that I can drop directly into a project and run (including XAML), or an actual application project. I'm trying to help a lot of people, so I don't have time to figure out weird snippets with undefined objects and unknown namespaces.

    Wednesday, September 24, 2014 1:00 PM
    Moderator
  • Have you try setting one CancellationTokenSource per request? If you use one, the token will expire after two seconds.
    Wednesday, September 24, 2014 3:36 PM
  • Oddly enough, that was the problem. Thank you very much for your input.
    Wednesday, September 24, 2014 5:46 PM
  • Hi José,

    Thanks for reporting this issue. As suggested by Stankiewicsz, a new CancellationTokenSource needs to be created and used on a per-request basis. Each request should get a fresh token source that issues tokens. Once a token has been canceled, it expires and cannot be used for subsequent requests.

    Can you please let us know where you found the guidance/code sample above? If it is in our documentation, we would be happy to correct it and make it clearer to understand.

    Thanks for your time and contribution.

    Sidharth

    Thursday, September 25, 2014 5:15 PM
  • Hello Sidharth,

    I found the answer in StackOverflow and MSDN Forums, however both used new a CancellationToken for each request. I thought that I could reuse it, hence my problem.
    I did find that the previous System.Web.Http.HttpClient had a Timeout property, and in terms of usability it seems a step back comparing to Windows.Web.Http.HttpClient. Apart from this I did not find a direct and clear association of the CancellationToken as a way to define the HttpClient timeout. Maybe this information should be clearer in the documentation, but that's just my opinion.

    Thank you for availability.

    Tuesday, September 30, 2014 2:24 PM