Note
Access to this page requires authorization. You can try signing in or changing directories.
Access to this page requires authorization. You can try changing directories.
Question
Tuesday, October 1, 2013 4:24 PM
I have the following test code. I always get the "Task was cancelled" error after looping 316934 or 361992 times.
If I am not wrong, there are two possible reasons why the task was cancelled a) HttpClient got timeout or b) too many tasks in queue and some tasks got time-out.
I couldn't find the documentation about the limitation in queueing the tasks. And I tried creating more than 500K tasks and no time-out. I guess the reason "b" might not be right.
Q1. Is there any other reason that I missed out?
Q2. If it's because HttpClient timeout, how can I get the exact exception message instead of "TaskCancellation" exception.
Q3. What would be the best way to fix it? Should I introduce the throttler?
Thanks!
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();
Michael Sync: blog: http://michaelsync.net
All replies (7)
Wednesday, October 2, 2013 3:07 AM
HI,
I believe this can occur when the timeout is exceeded. You might check your timeout, and log how long it was outstanding before the exception to see if it is being exceeded.
I think you could try writing an extension method to get the exception.
You can consider posting it in ASP.NET forum or WCF forum for more efficient responses.
Wednesday, October 2, 2013 4:10 AM
Hi v-hyper,
Thanks for reply!
The code that I wrote is in Console Application. I am doing http post to "google.com" from console application so I don't think it's nothing to do with ASP.NET or WCF.
I also think that it might be related to HttpClient.TimeOut section.
The problem is that I can't get the exception. Can you give me some points to get the exception message when using SendAsync()?
Thanks!
Michael Sync: blog: http://michaelsync.net
Wednesday, October 2, 2013 4:32 AM
How can I tell when HttpClient has timed out?
http://hc.apache.org/httpclient-3.x/apidocs/org/apache/commons/httpclient/HttpException.html
This will help you get some more information.
Monday, October 7, 2013 1:37 AM | 1 vote
How did you mark this as answered?
The links that v-hypo provided didn't answer my question at all..
I was asking how to get the actual exception message from the task that has been cancelled.
I provided the full source code to replicate the issue. You can simply create a console application to test it.
Michael Sync: blog: http://michaelsync.net
Monday, October 7, 2013 7:49 AM
Hey Michael,
I might not have the correct answer you need, I have tested your code, but I also can't get the exact error message.
How's it going? If you find the solution could you share it with me?
Monday, October 7, 2013 8:04 AM
Hello v-hypo,
Edited: I mis-read as you couldn't replicate the issue. :) Sorry. Yes. I couldn't get the exact message until.. That's why I am hoping someone might share me a way to get the exact message. Eason_H closed my thread so I was a bit pissed off.. :)
~~
Thanks for following up with me.
Here is the step to replicate the issue.
1. Create a Console Project in VS 2012.
2. Please copy and paste my code in Main.
3. Put the breakpoint at this line " log.ErrorException(string.Format("SoeHtike {0}", Task.CurrentId), ex);"
Run the program in debug mode. Wait for a few minutes. (maybe 5 minutes? ) I just tested my code and I got the exception after 3 mins. If you have fiddler, you can monitor the requests so that you know the program is still running or not.
Feel free to let me know if you can't replicate the issue.
Thanks again for your help.
Michael Sync: blog: http://michaelsync.net
Tuesday, October 8, 2013 2:48 AM
Hi Michael,
Thanks for the step, maybe we can't get the exact message.
I'm not familiar about async and await, your code is so perfect. I think you are an expert. Could you tell me how to study it, any ebooks or ideal is best.
Thanks!!!