WebAPI中的TaskScheduler.UnobservedTaskException取消了该操作

时间:2014-04-29 09:12:21

标签: asp.net-web-api scheduled-tasks unobserved-exception

最近我在我的后端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)

任何想法如何调试?

3 个答案:

答案 0 :(得分:6)

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

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

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

答案 1 :(得分: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;
    }

答案 2 :(得分:0)

有时也会获得TaskChanceledException,但我在Amazon Web Services船上。通常这是在我上传新版本的应用程序后立即发生的。通常app服务器重启(通过Beanstalk UI页面)帮助..