异步任务C#上的TaskCanceledException

时间:2017-09-29 08:47:10

标签: c# excel asynchronous task

我有一个带有以下代码的C#ConsoleApplication(代码段):

Task exportTask = Task.Run(async () =>
{
    try
    {
        // code
        IList<OmbisLeistungsPosition> leistungsPositionen = getItems();
        string result = await ombis.Write(leistungsPositionen);
        // code
    }
    catch (Exception e)
    {
        Email.SendErrorToDeveloper(e.ToString());
        Console.WriteLine(e.Message);
    }
});
exportTask.Wait();


internal async Task<string> Write(IList<OmbisLeistungsPosition> leistungsPositionen)
{
    HttpClient httpRequest = GetDigestHttpRequest();
    string json = JsonConvert.SerializeObject(leistungsPositionen);
    StringContent content = new StringContent(json, Encoding.UTF8, "application/json");
    HttpResponseMessage response = await httpRequest.PostAsync(UrlOmbisLeistungspositionen, content);
    string result = await response.Content.ReadAsStringAsync();
    return result;
}

PostAsync的预期结果是正常的,但是在调用PostAsync的行上我得到以下异常:

System.Threading.Tasks.TaskCanceledException: A task was canceled.
   at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)
   at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
   at System.Runtime.CompilerServices.TaskAwaiter`1.GetResult()
   at HGV.Personalberatung.Fakturierung770.Ombis.<Write>d__8.MoveNext() in C:\Users\EBorsoi\Documents\Git\HGV.Personalberatung.Fakturierung770.ServerApi\HGV.Personalberatung.Fakturierung770\Ombis.cs:line 229
--- End of stack trace from previous location where exception was thrown ---
   at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
   at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)
   at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
   at System.Runtime.CompilerServices.TaskAwaiter`1.GetResult()
   at HGV.Personalberatung.Fakturierung770.Program.<>c__DisplayClass3_0.<<ExportToOmbis>b__0>d.MoveNext() in C:\Users\EBorsoi\Documents\Git\HGV.Personalberatung.Fakturierung770.ServerApi\HGV.Personalberatung.Fakturierung770\Program.cs:line 109

为什么系统会抛出异常?这是什么意思?我该如何解决?

0 个答案:

没有答案