11

AggregateException is throwing while waiting for API post to complete how i could fix this?

My API call is similar like this

using (var httpClient = new HttpClient())
{
    httpClient.BaseAddress = new Uri(workflowUrl);
    var task = httpClient.PostAsJsonAsync("api/apiname/execute/", executeModel)
                             .ContinueWith(x => x.Result.Content.ReadAsAsync<bool>().Result);

    Task continuation = task.ContinueWith(x =>
    {
        bool response = x.Result;
    });
    continuation.Wait(); 

}

I am getting Exception given below while waitng for POST to complete.

  System.AggregateException was caught
  Message=One or more errors occurred.
  Source=mscorlib
  StackTrace:
       at System.Threading.Tasks.Task.ThrowIfExceptional(Boolean includeTaskCanceledExceptions)
       at System.Threading.Tasks.Task.Wait(Int32 millisecondsTimeout, CancellationToken cancellationToken)
       at System.Threading.Tasks.Task.Wait()
       at 
  InnerException: System.AggregateException
       Message=One or more errors occurred.
       Source=mscorlib
       StackTrace:
            at System.Threading.Tasks.Task.ThrowIfExceptional(Boolean includeTaskCanceledExceptions)
            at System.Threading.Tasks.Task`1.get_Result()
            at 
            at System.Threading.Tasks.Task`1.<>c__DisplayClass17.<ContinueWith>b__16(Object obj)
            at System.Threading.Tasks.Task.InnerInvoke()
            at System.Threading.Tasks.Task.Execute()
       InnerException: System.AggregateException
            Message=One or more errors occurred.
            Source=mscorlib
            StackTrace:
                 at System.Threading.Tasks.Task.ThrowIfExceptional(Boolean includeTaskCanceledExceptions)
                 at System.Threading.Tasks.Task`1.get_Result()
                 at WebUI.ListController.<ListRepeater_ItemCommand>b__f(Task`1 x) in PostAsJsonAsync:line 741
                 at System.Threading.Tasks.Task`1.<>c__DisplayClass1a`1.<ContinueWith>b__19()
                 at System.Threading.Tasks.Task`1.InvokeFuture(Object futureAsObj)
                 at System.Threading.Tasks.Task.InnerInvoke()
                 at System.Threading.Tasks.Task.Execute()
            InnerException: System.Threading.Tasks.TaskCanceledException
                 Message=A task was canceled.
                 InnerException:
Kara
  • 6,115
  • 16
  • 50
  • 57
niknowj
  • 977
  • 4
  • 19
  • 37
  • Are you actually getting the exception with this code? Because it looks like you're using cancellation in your real code and I don't see that here. – svick Dec 06 '13 at 10:39
  • I had the same issue and could not resolve it. I ended up using an HttpWebRequest to avoid this problem. The request was not timing out. I debugged the web service and also watched the requests go out and come back with Fiddler, returning an OK / 200 status code. The post data was very small. – Dudeman3000 Oct 09 '14 at 13:44

1 Answers1

18

Are you posting a big piece of data ? Once I had a similar issue. I believe the underlying problem is occasional timeout due to bandwidth issues. I tried getting the task and manually waiting on it then checking .IsFaulted and .IsCancelled but the .Result throws AggregateException / TaskCancelledException.

Maybe you should try to increase HttpClient.Timeout property which is 100 sec by default ?

Tomasz Jaskuλa
  • 15,723
  • 5
  • 46
  • 73
  • 6
    Thanks for the tip, it was driving me crazy. I don't know why they don't throw something more sensible, like "timeout expired". – Marcel Popescu Dec 16 '13 at 10:06