HttpRequestException while executing client.SendAsync

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

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 (maybe 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 limitation that Task.WhenAll can handle?

+3


source to share


1 answer


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

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

      

And you are already in the queue, alternately waiting RunRequest

, sequentially, so nothing is triggered on the parallel, and yours Task.WhenAll

should throw an exception as you represent it as HttpResponseMessage

, not Task

.



RunRequest

does not return bool

, so how do you use it in if

?

... there are a lot of errors in this code ...

0


source







All Articles