2015-08-08 68 views
5

当我使用对Web Api的调用的客户端时发生错误。问题是我回来的堆栈跟踪有点难以理解,没有行号,所以我不确定在哪里寻找。这是作为Json响应返回的。从Asp.Net Web Api正确堆栈跟踪异步/任务调用

"message": "An error has occurred.", 
"exceptionMessage": "Object reference not set to an instance of an object.", 
"exceptionType": "System.NullReferenceException", 
"stackTrace": " at WakeSocial.Gateway.WebApi.Host.Controllers.NotificationsController.<Response>d__7.MoveNext()\r\n--- End of stack trace from previous location where exception was thrown ---\r\n at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)\r\n at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)\r\n at System.Threading.Tasks.TaskHelpersExtensions.<CastToObject>d__3`1.MoveNext()\r\n--- End of stack trace from previous location where exception was thrown ---\r\n at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)\r\n at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)\r\n at System.Web.Http.Controllers.ApiControllerActionInvoker.<InvokeActionAsyncCore>d__0.MoveNext()\r\n--- End of stack trace from previous location where exception was thrown ---\r\n at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)\r\n at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)\r\n at System.Web.Http.Controllers.ActionFilterResult.<ExecuteAsync>d__2.MoveNext()\r\n--- End of stack trace from previous location where exception was thrown ---\r\n at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)\r\n at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)\r\n at System.Web.Http.Filters.AuthorizationFilterAttribute.<ExecuteAuthorizationFilterAsyncCore>d__2.MoveNext()\r\n--- End of stack trace from previous location where exception was thrown ---\r\n at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)\r\n at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)\r\n at System.Web.Http.Controllers.AuthenticationFilterResult.<ExecuteAsync>d__0.MoveNext()\r\n--- End of stack trace from previous location where exception was thrown ---\r\n at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)\r\n at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)\r\n at System.Web.Http.Dispatcher.HttpControllerDispatcher.<SendAsync>d__1.MoveNext()" 
} 

有没有一种方法可以配置web api来返回发生错误的行号,以便我可以更合适地调试问题?

的问题是这里介绍:Stack traces with async/await

我目前USNG .NET 4.5然而,错误仍然可以输出这种方式。

+0

你可以调试api吗?如果可以的话,您可以轻松查找“NullReferenceException”。 –

+2

@JoelDean:'WakeSocial.Gateway.WebApi.Host.Controllers.NotificationsController。 d__7.MoveNext()'表示它在'NotificationsController.Response'动作中。行动是否真的很漫长和复杂,以至于你需要一个行号来追踪它? –

回答

0

我使用其存储内部异常堆栈跟踪信息的扩展,例如,原来的代码:

try{  
    await NestedAsyncOperation(); 
}catch(NullReferenceException e){ 
    Debug.WriteLine(e) 
} 

与可读堆栈跟踪:

try{  
    await NestedAsyncOperation().Trace(); 
}catch(Exception e){ 
    e.Catch((NullReferenceException ex) => { 
    Debug.WriteLine(e); 
    }).RethrowUnhandled(); 
} 

和输出将包含具有线栈跟踪数字。 查看更多https://github.com/ilio/AsyncStackTrace/blob/master/UnitTests/AsyncStackTraceExtensionUnitTest.cs