我在 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。难道我做错了什么?是否有另一种模式可以让我获得更完整的堆栈跟踪?
编辑:我的目标不是编写依赖于堆栈跟踪的代码,而是使异步方法中的故障更容易调试。