Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Asp.NET Web API "The operation was canceled." exception

I'm working with a Web API that handles any exception occured with a FilterAttribute.

In WebApiConfig.cs file, it is registered:

  config.Filters.Add(new ExceptionHandlerAttribute());

It works correctly and any time an unhandled exception is raised it enters to the filter attribute OnException method:

public class ExceptionHandlerAttribute : ExceptionFilterAttribute
{
    public override void OnException(HttpActionExecutedContext context)
    {(...)}
}

Now, I have a client app that calls the API with a very small timeout intentionally. It seems everytime the timeout is expired, it affects in some way the server, and the following exception is catched in the FilterAttribute:

context: System.Web.Http.Controllers.HttpActionContext
"The operation was canceled."

at System.Threading.CancellationToken.ThrowOperationCanceledException()
at System.Threading.CancellationToken.ThrowIfCancellationRequested()
at System.Net.Http.HttpContentExtensions.<ReadAsAsyncCore>d__0`1.MoveNext()

The stacktrace doesn't display any code line in the server project. It seems that when the client call is cancelled it raises this exception at Server side? Is it an expected exception/behaviour?

If it is expected, I just have to ignore this kind of exception raised by the cancelled client call?

Thanks in advance.

like image 651
Alberto Montellano Avatar asked Nov 10 '22 17:11

Alberto Montellano


1 Answers

This issue occurs when you are trying to cancel the operation before its completion. Debug your code and see if any of your webAPI method is called but not completed. Hope this helps.

like image 96
anupriya gupta Avatar answered Nov 15 '22 07:11

anupriya gupta