Typed http client throws socket exception if to use it along with Task.WhenAll()

0

I have typed Http client with Polly configured. This is my Program.cs:

       serviceCollection.AddSingleton<IPolicyBuilder, PolicyBuilder>();
       serviceCollection
                .AddHttpClient<ICustomHttpClient, CustomHttpClient>()
                .AddPolicyHandler((serviceProvider, httpRequestMessage) =>
                {
                    var policyBuilder = serviceProvider.GetRequiredService<IPolicyBuilder>();
                    return policyBuilder.BuildGenericPolicy();
                });

My typed client:

    public class CustomHttpClient: ICustomHttpClient
    {
        private readonly HttpClient _httpClient;

        public NotificationsApiHttpClient(HttpClient httpClient)
        {
            _httpClient = httpClient ?? throw new ArgumentNullException(nameof(httpClient));
        }

        public async Task<HttpResponseMessage> SendEmailCommandAsync(string jsonEmailCommand)
        {
            return await _httpClient.PostAsync(new Uri("dummy uri"), new StringContent("dummy content"));
        }

I need to send the batch of HTTP requests and I'm not interested in responses. I inject my typed CustomHttpClient. In order to increase perfomance, I've tried to do this:

var tasks = new List<Task>();

foreach(item in collection)
{
  tasks.Add(_customHttpClient.SendEmailCommandAsync("dummyJsonContent");
}

await Task.WhenAll(tasks);

If I have around 100+ items in my array I always get this exception:

System.Threading.Tasks.TaskCanceledException
  HResult=0x8013153B
  Message=The operation was canceled.
  Source=System.Net.Http
  StackTrace:
   at System.Net.Http.HttpConnection.<SendAsyncCore>d__61.MoveNext()
   at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
   at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)
   at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
   at System.Runtime.CompilerServices.ConfiguredTaskAwaitable`1.ConfiguredTaskAwaiter.GetResult()
   at System.Net.Http.HttpConnectionPool....

Inner Exception 1:
IOException: Unable to read data from the transport connection: The I/O operation has been aborted because of either a thread exit or an application request.

Inner Exception 2:
SocketException: The I/O operation has been aborted because of either a thread exit or an application request

PS. Sorry had to shrink stacktrace a bit since it's too long

And it works without any exceptions with this:

foreach(item in collection)
{
  await _customHttpClient.SendEmailCommandAsync("dummyJsonContent");
}
c#
performance
.net-core
dotnet-httpclient
asked on Stack Overflow Jul 18, 2019 by OlegI • edited Jul 18, 2019 by OlegI

0 Answers

Nobody has answered this question yet.


User contributions licensed under CC BY-SA 3.0