首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >等待PostAsJsonAsync时AggregateException抛出

等待PostAsJsonAsync时AggregateException抛出
EN

Stack Overflow用户
提问于 2013-12-06 14:22:33
回答 1查看 22.2K关注 0票数 11

AggregateException在等待API post完成时抛出了问题,我该如何解决这个问题呢?

我的API调用类似如下

代码语言:javascript
复制
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完成时,我得到了下面给出的异常。

代码语言:javascript
复制
  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:
EN

回答 1

Stack Overflow用户

发布于 2013-12-06 19:14:41

你在张贴一大段数据吗?有一次我遇到了类似的问题。我认为潜在的问题是偶尔由于带宽问题而导致的超时。我尝试获取任务并手动等待,然后检查.IsFaulted.IsCancelled,但.Result抛出AggregateException / TaskCancelledException

也许你应该试着增加HttpClient.Timeout属性,默认是100秒?

票数 18
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/20417383

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档