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?
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With