HttpClient的 - 任务被取消 - 如何获得确切的错误信息? [英] HttpClient - task was cancelled - How to get the exact error message?

查看:2386
本文介绍了HttpClient的 - 任务被取消 - 如何获得确切的错误信息?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下的测试code。我总是得到任务已被取消错误循环316934或361992次。

I have the following test code. I always get the "Task was cancelled" error after looping 316934 or 361992 times.

如果我没有错,为什么有任务被取消了)的HttpClient有超时或b)在队列中太多的任务,有些任务得到超时可能有两个原因。

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.

我找不到关于排队的任务限制的文档。我试图创造了超过50万的任务,没有超时。我想原因B可能是不对的。

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。是否有我错过了任何其他原因?

Q1. Is there any other reason that I missed out?

Q2。如果是因为HttpClient的超时,我怎样才能得到确切的异常信息,而不是TaskCancellation异常。

Q2. If it's because HttpClient timeout, how can I get the exact exception message instead of "TaskCancellation" exception.

Q3。什么是解决它的最好方法?我应该引入了节流?

Q3. What would be the best way to fix it? Should I introduce the throttler?

谢谢!

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();

下面是复制这一问题的步骤。

Here is the step to replicate the issue.


  1. 在2012年VS创建一个控制台项目

  1. Create a Console Project in VS 2012.

请复制并粘贴在我的主要code。

Please copy and paste my code in Main.

把断点在该行log.ErrorException(的String.Format(SoeHtike
{0},
Task.CurrentId)
前);

Put the breakpoint at this line " log.ErrorException(string.Format("SoeHtike {0}", Task.CurrentId), ex);"

以调试模式运行该程序。等待几分钟。 (也许5分钟呢?)我刚刚测试了我的code和我后3分钟得到了异常。如果你有小提琴手,可以监视,让你知道该程序仍在运行与否的请求。

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.

推荐答案

默认 HttpClient.Timeout 值为100秒(00:01:40)。如果你这样做在你的抓时间戳块,你会发现任务开始得到正好那个时候取消。显然有每秒可以做的HTTP请求的数量有限,他人得到排队。排队的请求被取消的超时。出任务的所有600K的,我个人得到了仅2500只成功了,其他人被取消了。

The default HttpClient.Timeout value is 100 seconds (00:01:40). If you do a timestamp in your catch block you will notice that tasks begin to get canceled at exactly that time. Apparently there is a limited number of HTTP requests you can do per second, others get queued. Queued requests get canceled on timeout. Out of all 600k of tasks I personally got only 2500 successful, others got canceled.

我也觉得不太可能,你将能够运行的任务,整个60万。许多网络驱动程序让通过高数只为一个小的时间的请求,并减少编号以一段时间后,一个非常低的值。我的网卡让我到36秒内只发送921请求,并丢弃的速度每秒只有一个请求。在这样的速度将需要一个星期才能完成所有任务。

I also find it unlikely, that you will be able to run the whole 600000 of tasks. Many network drivers let through high number of requests only for a small time, and reduce that number to a very low value after some time. My network card allowed me to send only 921 requests within 36 seconds and dropped that speed to only one request per second. At that speed it will take a week to complete all the tasks.

如果你能绕过这个限制,请确保您打造code为64位平台的应用程序是内存饿极了。

If you are able to bypass that limitation, make sure you build the code for 64-bit platform as the app is very hungry for memory.

这篇关于HttpClient的 - 任务被取消 - 如何获得确切的错误信息?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆