Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

HttpClient: Count cannot be less than zero. Parameter name: count

Tags:

c#

I use HttpClient to perform REST calls to remote server:

using (var response = httpClient.PostAsync(Url, content).Result)
{
// code
}

It works fine, but sometimes it returns strange error:

2014-07-28 12:04:40,098 (268795336) [114] ERROR - System.AggregateException: One or more errors occurred. ---> System.Net.Http.HttpRequestException: An error occurred while sending the request. ---> System.Net.WebException: The underlying connection was closed: An unexpected error occurred on a send. ---> System.ArgumentOutOfRangeException: Count cannot be less than zero. Parameter name: count at System.Net.TlsStream.EndWrite(IAsyncResult asyncResult) at System.Net.ConnectStream.WriteHeadersCallback(IAsyncResult ar)

What could be the cause of the problem?

like image 860
Vasil Galinovsky Avatar asked Jul 28 '14 14:07

Vasil Galinovsky


1 Answers

I guess the occasional problem might be caused by a deadlock. When we are using async methods, it is better to use await rather than Task.Result for the above reason. Do take a look at this link which discuss about best practices in asynchronous programming.

like image 83
Yuan Shing Kong Avatar answered Nov 14 '22 23:11

Yuan Shing Kong