The operation was canceled at TaskScheduler.UnobservedTaskException in WebAPI

那年仲夏 提交于 2019-12-10 13:47:05

问题


Recently I have added error logging for every UnobservedTaskException at my backend API app. The reason is that some API call executes additional tasks which are finished AFTER I return the result, that is why I couldn't track errors there on action (web api calls) level.

Now I'm getting lots of "The operation was canceled" exceptions and not sure what to do with it. I even not sure that this exception is caused by my tasks executed by API call.

Here is stack:

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)

Any ideas how to debug it?


回答1:


Task cancel exception comes typically from tasks from an aborted request. We are tracking a bug that will be fixed in 5.2 where these are not going to show up in the exception logger. So typically you can safely ignore these.

You can pick up the nightly builds on https://www.myget.org/gallery/aspnetwebstacknightly to verify this fixes your issue.

Here is a link to the bug: http://aspnetwebstack.codeplex.com/workitem/1797




回答2:


You can ignore this type of error

 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);
    }

And in Web API controller

[ExceptionHandling]
  public class SomeController : ApiController
  {
    [HttpGet]
    public object GetSomething()
    {
      return something;
    }



回答3:


Getting TaskChanceledException too sometimes, but i am at Amazon Web Services boat. Usually this happens right after I uploaded new version of app. Usually app servers restart (via Beanstalk UI page) helps..



来源:https://stackoverflow.com/questions/23360375/the-operation-was-canceled-at-taskscheduler-unobservedtaskexception-in-webapi

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!