I have the following test code. I always get the "The task was canceled" error after a cycle of 316934 or 361992 times.
If I am not mistaken, there are two possible reasons why the task was canceled. a) HttpClient received a timeout or b) there are too many tasks in the queue, and some tasks have timed out.
I could not find the documentation about the restriction in the sequence of tasks. And I tried to create more than 500 thousand tasks and without a timeout. I think reason "b" may be wrong.
Q1. Is there another reason I missed?
Q2. If this is due to the HttpClient timeout, how can I get the exact exception message instead of the TaskCancellation exception.
Q3. What would be the best way to fix this? Should I introduce a throttle?
Thank!
var _httpClient = new HttpClient(); _httpClient.DefaultRequestHeaders.TryAddWithoutValidation("Accept", "text/html,application/xhtml+xml,application/xml"); _httpClient.DefaultRequestHeaders.TryAddWithoutValidation("Accept-Encoding", "gzip, deflate"); _httpClient.DefaultRequestHeaders.TryAddWithoutValidation("User-Agent", "Mozilla/5.0 (Windows NT 6.2; WOW64; rv:19.0) Gecko/20100101 Firefox/19.0"); _httpClient.DefaultRequestHeaders.TryAddWithoutValidation("Accept-Charset", "ISO-8859-1"); int[] intArray = Enumerable.Range(0, 600000).ToArray(); var results = intArray .Select(async t => { using (HttpRequestMessage requestMessage = new HttpRequestMessage(HttpMethod.Get, "http://www.google.com")) { log.Info(t); try { var response = await _httpClient.SendAsync(requestMessage); var responseContent = await response.Content.ReadAsStringAsync(); return responseContent; } catch (Exception ex) { log.ErrorException(string.Format("SoeHtike {0}", Task.CurrentId), ex); } return null; } }); Task.WaitAll(results.ToArray()); Console.ReadLine();
The following is a step to replicate the problem.
Create a console project in VS 2012.
Copy and paste my code into Main.
Place a breakpoint on this line "log.ErrorException (string.Format (" SoeHtike {0} ", Task.CurrentId), ex);"
Run the program in debug mode. Wait a few minutes. (maybe 5 minutes?) I just checked my code and I got an exception after 3 minutes. If you have a violinist, you can track requests so that you know that the program is still working or not.
Feel free to let me know if you can not reproduce the problem.
Michael Sync Oct 01 '13 at 15:52 2013-10-01 15:52
source share