7

最近,我在后端 API 应用程序中为每个 UnobservedTaskException 添加了错误日志记录。原因是某些 API 调用执行了在我返回结果后完成的附加任务,这就是为什么我无法在操作(web api 调用)级别跟踪错误的原因。

现在我收到很多“操作被取消”的异常,不知道该怎么办。我什至不确定这个异常是由我的 API 调用执行的任务引起的。

这是堆栈:

System.OperationCanceledException: The operation was canceled.
   at System.Threading.CancellationToken.ThrowIfCancellationRequested()
   at System.Web.Http.WebHost.HttpControllerHandler.<WriteBufferedResponseContentAsync>d__1b.MoveNext()
--- End of stack trace from previous location where exception was thrown ---
   at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
   at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)
   at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
   at System.Web.Http.WebHost.HttpControllerHandler.<CopyResponseAsync>d__7.MoveNext()
--- End of stack trace from previous location where exception was thrown ---
   at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
   at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)
   at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
   at System.Web.Http.WebHost.HttpControllerHandler.<ProcessRequestAsyncCore>d__0.MoveNext()
--- End of stack trace from previous location where exception was thrown ---
   at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
   at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)
   at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
   at System.Web.TaskAsyncHelper.EndTask(IAsyncResult ar)
   at System.Web.HttpApplication.CallHandlerExecutionStep.OnAsyncHandlerCompletion(IAsyncResult ar)

任何想法如何调试它?

4

3 回答 3

8

任务取消异常通常来自中止请求的任务。我们正在跟踪将在 5.2 中修复的错误,这些错误不会出现在异常记录器中。所以通常你可以放心地忽略这些。

您可以在https://www.myget.org/gallery/aspnetwebstacknightly上获取每晚构建,以验证这是否解决了您的问题。

这是该错误的链接:http: //aspnetwebstack.codeplex.com/workitem/1797

于 2014-04-29T14:34:01.253 回答
1

您可以忽略此类错误

 public class ExceptionHandlingAttribute : ExceptionFilterAttribute
  {
    public override void OnException(HttpActionExecutedContext context)
    {
      if (context.Exception.Message == "The operation was canceled.")
        return;
      var title = "friendly  title";
      var msg = "friendly customizable message";
      NLog.Logger logger = NLog.LogManager.GetCurrentClassLogger();
      logger.Fatal(context.Exception);
      var resp = new HttpResponseMessage(HttpStatusCode.Gone)
        {
          Content = new StringContent(title),
          ReasonPhrase = msg
        };
      throw new HttpResponseException(resp);
    }

并在 Web API 控制器中

[ExceptionHandling]
  public class SomeController : ApiController
  {
    [HttpGet]
    public object GetSomething()
    {
      return something;
    }
于 2014-09-23T08:29:28.887 回答
0

有时也会收到 TaskChanceledException,但我在亚马逊网络服务船上。通常这发生在我上传新版本的应用程序之后。通常应用程序服务器重新启动(通过 Beanstalk UI 页面)会有所帮助..

于 2015-02-02T15:19:18.693 回答