Everything was going along fine, and now my HttpClient.PostAsync throws an AggregateException containing a WebException stating, "The request was canceled". What would be causing this? Thanks!
The "using" block will be disposing of the HttpClient instance before it's necessarily completed. The continuation is executed asynchronously, meaning the "using" block is very quickly finished, while the HttpClient is still attempting to operate on a background
thread. It's basically doing:
Create HttpClient.
Run PostAsync.
Get to end of "using" block - dispose HttpClient.
PostAsync hasn't finished yet, but gets cancelled by HttpClient.Dispose().
bittondb
Member
17 Points
18 Posts
All of a Sudden HttpClient PostAsync is Being Canceled
Mar 01, 2012 08:04 PM|LINK
Everything was going along fine, and now my HttpClient.PostAsync throws an AggregateException containing a WebException stating, "The request was canceled". What would be causing this? Thanks!
bittondb
Member
17 Points
18 Posts
Re: All of a Sudden HttpClient PostAsync is Being Canceled
Mar 01, 2012 08:07 PM|LINK
Sorry, here's the code:
using (var client = new HttpClient(new PayPalMessageHandler(new HttpClientHandler()))) { client.Timeout = TimeSpan.FromSeconds(90); client.PostAsync(Address, content).ContinueWith(requestTask => { var response = requestTask.Result; response.EnsureSuccessStatusCode(); response.Content.ReadAsAsync<JsonObject>(). ContinueWith( readTask => { var o = readTask.Result; Console.WriteLine("{0}", o); }); }); }bittondb
Member
17 Points
18 Posts
Re: All of a Sudden HttpClient PostAsync is Being Canceled
Mar 01, 2012 08:08 PM|LINK
Nevermind! I added the using at some point. I removed it and it's all good!
skmcfadden
Member
36 Points
99 Posts
Re: All of a Sudden HttpClient PostAsync is Being Canceled
Jan 07, 2013 02:30 PM|LINK
Just curious... Why would the using block cause aggregate exception?
Snixtor
Member
362 Points
88 Posts
Re: All of a Sudden HttpClient PostAsync is Being Canceled
May 28, 2013 01:39 AM|LINK
The "using" block will be disposing of the HttpClient instance before it's necessarily completed. The continuation is executed asynchronously, meaning the "using" block is very quickly finished, while the HttpClient is still attempting to operate on a background thread. It's basically doing: