HttpRequestException when doing a client.SendAsync

Deadly 提交于 2019-12-07 13:54:49

问题


All of a sudden, this piece of code that usually works started throwing HttpRequestException errors. In the logs, I see that the request was actually sent 1 minute and 35 seconds before the error was thrown. Could it be a timeout issue?

Here is the code:

private async Task<HttpResponseMessage> RunRequest(HttpRequestMessage request)
{            
    var client = new HttpClient();
    var response = await client.SendAsync(request).ConfigureAwait(false);              
    return response;           
}

Here is the caller (there could between 10K to 50K items):

int counter = 0;
var tasks = items.Select(async i =>
{
    if (await RunRequest(CreateRequest(i)))
        counter++
}).ToList();

if (tasks.Any())
{ 
    await Task.WhenAll(tasks);
}

Here is the error:

System.Net.Http.HttpRequestException: An error occurred while sending the request. ---> System.Net.WebException: The request was aborted: The request was canceled.
        at System.Net.HttpWebRequest.EndGetResponse(IAsyncResult asyncResult)
        at System.Net.Http.HttpClientHandler.GetResponseCallback(IAsyncResult ar)
        --- End of inner exception stack trace ---
        at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)
        at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
        at MyClass.<RunRequest>d__c.MoveNext()
        --- End of inner exception stack trace ---
        at MyClass.<RunRequest>d__c.MoveNext()
        --- End of stack trace from previous location where exception was thrown ---
        at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)
        at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
        at MyClass.<RunRequest>d__0.MoveNext()

Is there a limit that Task.WhenAll can handle?


回答1:


If all your requests are to the same domain, then you need to add this to your "Web.config" file (default is just 2):

<configuration>
    <system.net>
        <connectionManagement>
            <add address="*" maxconnection="100" />
        </connectionManagement>
    <system.net>
<configuration>

And you are already awaiting on every single RunRequest in turn, serially, so nothing is actually run in paralell - and your Task.WhenAll should throw an exception since you are givin it a HttpResponseMessage list instead of a Task list.

RunRequest does not return a bool, so how are you using it in an if?

...there is just so much wrong with this code...



来源:https://stackoverflow.com/questions/25213414/httprequestexception-when-doing-a-client-sendasync

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!