gpt4 book ai didi

c# - 等待 PostAsJsonAsync 时抛出 AggregateException

转载 作者:可可西里 更新时间:2023-11-01 08:40:40 26 4
gpt4 key购买 nike

在等待 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:

最佳答案

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

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

关于c# - 等待 PostAsJsonAsync 时抛出 AggregateException,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20417383/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com