相关疑难解决方法(0)

是否可以使用.NET异步方法获得良好的堆栈跟踪?

我在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!");
}
Run Code Online (Sandbox Code Playgroud)

可悲的是,当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"
Run Code Online (Sandbox Code Playgroud)

因此,我在跟踪中得到(损坏)GetValue2和GetValue,但没有提到GetValueAction.难道我做错了什么?是否有另一种模式可以让我获得更完整的堆栈跟踪?

编辑:我的目标不是编写依赖于堆栈跟踪的代码,而是使异步方法中的失败更容易调试.

c# stack-trace async-await asp.net-web-api

52
推荐指数
3
解决办法
1万
查看次数

标签 统计

asp.net-web-api ×1

async-await ×1

c# ×1

stack-trace ×1