2015-08-08 78 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