【问题标题】:AggregateException is throwing while waiting for PostAsJsonAsync等待 PostAsJsonAsync 时抛出 AggregateException
【发布时间】:2013-12-06 06:22:32
【问题描述】:

在等待 API 发布完成时抛出 AggregateException 我该如何解决这个问题?

我的 API 调用类似这样

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(); 

}

我在等待 POST 完成时收到下面给出的异常。

  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:

【问题讨论】:

  • 你真的得到了这段代码的异常吗?因为看起来您在真实代码中使用了取消功能,而我在这里看不到。
  • 我遇到了同样的问题,无法解决。我最终使用 HttpWebRequest 来避免这个问题。请求没有超时。我调试了 Web 服务,还观察了请求发出并与 Fiddler 一起返回,返回一个 OK / 200 状态代码。帖子数据非常少。

标签: c# asp.net asp.net-web-api task-parallel-library


【解决方案1】:

您要发布大量数据吗?曾经我遇到过类似的问题。我相信潜在的问题是由于带宽问题偶尔超时。我尝试获取任务并手动等待它然后检查.IsFaulted.IsCancelled.Result 抛出AggregateException / TaskCancelledException

也许您应该尝试增加默认为 100 秒的 HttpClient.Timeout 属性?

【讨论】:

  • 感谢您的提示,这让我发疯了。我不知道他们为什么不抛出更明智的东西,比如“超时已过期”。
猜你喜欢
  • 2012-08-14
  • 1970-01-01
  • 2013-08-21
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-04-20
相关资源
最近更新 更多