HttpClient(C#)在许多异步请求上失败? [英] HttpClient (C#) fails on many asynchronous requests?

查看:283
本文介绍了HttpClient(C#)在许多异步请求上失败?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用HttpClient异步向外部api发送许多请求。我等待所有请求完成,然后在其他代码中使用响应。我的问题是,如果我发出太多请求,则当我使用Task.WhenAll等待时,我的代码将引发异常。

I'm using HttpClient to asynchronously make many requests to an external api. I wait for all the requests to complete, then use the responses in other code. My problem is that if I make too many requests, my code throws an exception when I use the Task.WhenAll to wait.

此代码最终将并行运行,我的意思是,我将同时处理许多异步请求(即10套200个异步请求)。我实例化了一个与.NET 4.5异步/等待修饰符一起使用的HttpClient,如下所示:

This code will ultimately be run in parallel, by which I mean that I'll be doing many sets of these async requests at the same time (i.e. 10 sets of 200 async requests). I've instantiated an HttpClient that I'm using with .NET 4.5 async/await modifiers like this:

using (var client = new HttpClient())
{
    // make a list of tasks
    List<Task<HttpResponseMessage>> taskList;
    List<MyData> replies;
    for (int i = 0; i < MAX_NUMBER_REQUESTS; i++)
    {
        taskList.Add(client.GetAsync(externalUri);
    }

    List<HttpResponseMessage> responses = await Task.WhenAll(taskList);

    // read each response after they have returned
    foreach (var response in responses)
    {
        var reader = new System.IO.StreamReader(await response.Content.ReadAsStreamAsync());
        replies.Add(JsonConvert.DeserializeObject<MyData>(reader.ReadToEnd()));
        reader.Close();
} 

    foreach (var reply in replies)
    {
        // do something with the response from the external api call...
    }
}

我一直收到TaskCanceledException。这可能是一个超时问题。我不知道如何解决它。在使用Task.WhenAll并重复执行之前,我尝试以100个请求来批量处理我的请求。 n当我与三组100个失败的请求并行运行时。我是否缺少某些东西,是否有人对此有任何见识?

I keep getting a TaskCanceledException. After looking into this I saw this is possibly a timeout issue. I have no idea how to fix it. I attempted to batch my requests at 100 requests before using the Task.WhenAll and repeating, which worked. Then when I ran that in parallel with three sets of 100 requests that fails. Am I missing something, does anyone have any insight into this?

推荐答案

调整 ServicePointManager.DefaultConnectionLimit 。对于大量并发的请求,您可以将其设置为 int.MaxValue

这篇关于HttpClient(C#)在许多异步请求上失败?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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