【发布时间】:2013-03-02 21:12:59
【问题描述】:
我在 WebApi 应用程序中设置了以下示例代码:
[HttpGet]
public double GetValueAction()
{
return this.GetValue().Result;
}
public async Task<double> GetValue()
{
return await this.GetValue2().ConfigureAwait(false);
}
public async Task<double> GetValue2()
{
throw new InvalidOperationException("Couldn't get value!");
}
可悲的是,当 GetValueAction 被命中时,返回的堆栈跟踪是:
" at MyProject.Controllers.ValuesController.<GetValue2>d__3.MoveNext() in c:\dev\MyProject\MyProject\Controllers\ValuesController.cs:line 61 --- End of stack trace from previous location where exception was thrown ---
at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)
at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
at System.Runtime.CompilerServices.ConfiguredTaskAwaitable`1.ConfiguredTaskAwaiter.GetResult()
at MyProject.Controllers.ValuesController.<GetValue>d__0.MoveNext() in c:\dev\MyProject\MyProject\Controllers\ValuesController.cs:line 56"
因此,我在跟踪中得到(损坏的)GetValue2 和 GetValue,但没有提及 GetValueAction。难道我做错了什么?是否有另一种模式可以让我获得更完整的堆栈跟踪?
编辑:我的目标不是编写依赖于堆栈跟踪的代码,而是使异步方法中的故障更容易调试。
【问题讨论】:
-
你可能想看看异步因果链上的this article。
-
@ChaseMedallion:请查看我的更新答案。
-
不是直接的答案,但this answer 给出了如何破译给定异步堆栈帧的原始方法的提示。
标签: c# asp.net-web-api stack-trace async-await