我正在使用任务并行库来运行一个任务,该任务在取消时会引发 OperationCanceledException,然后使用 AggregateException 将其捕获,如下所示。AggregateException 包含一个 TaskCanceledExceptions 列表,它们对应于抛出的异常。不幸的是,这些 TaskCanceledExceptions 似乎正在丢失原始异常引发的堆栈跟踪。这是设计使然吗?
try
{
task1.Wait();
}
catch (AggregateException aggEx)
{
var tcex = ex as TaskCanceledException;
if (tcex != null)
{
Debug.WriteLine("InnerException:{0}, Message:{1}, Source:{2}, StackTrace: {3}",
tcex.InnerException, tcex.Message, tcex.Source, tcex.StackTrace);
return true;
}
else
{
return false;
}
}
结果:
InnerException:, Message:A task was canceled., Source:, StackTrace: