user25652029
user25652029

Reputation: 1

The Web API responds to the browser before the await Task.Delay finished executing

I'm working on an OWIN ASP.NET Web API 2 application which uses Autofac.

I ran into some issues because some code after await is never executed when the client app (browser) is cancelling the request.

Expected flow:

  1. The browser initiates the request
  2. The server starts executing a long-running action (await Task.Delay(10000) - 10 seconds)
  3. The browser aborts the request after 5 seconds.
  4. The server finishes the Task.Delay execution.
  5. The ContinueWith and the code after "await Task.Delay(10000)" is executed.

I've tested and received the expected behavior for a couple of different project setups: Web API 2, Web API 2 with Autofac, Web API 2 with OWIN integration.

Actual flow:

  1. The browser initiates the request
  2. The server starts executing a long-running action (await Task.Delay(10000) - 10 seconds)
  3. The browser aborts the request after 5 seconds.
  4. The server sends a 500 response immediately after that without waiting for the Task.Delay to complete.
  5. After 10 seconds only the ContinueWith method is executed.

I've received this behavior only for ASP.NET Web API 2 with Owin Integration + Autofac.

Here is my code:

[HttpGet]
[Route("test")]
public async Task<string> GetAll()
{
    await DoLongRunningOperationAsync(CancellationToken.None);

    return "Success";
}

private async Task DoLongRunningOperationAsync(CancellationToken cancellationToken)
{
    try
    {
        // Log start of delay
        Debug.WriteLine("Starting delay");
        
        await Task.Delay(10000, cancellationToken)
            .ContinueWith(task =>
            {
                Debug.WriteLine("Continue With");
            });

        // Log successful completion of delay
        Debug.WriteLine("Delay completed successfully");
    }
    catch (OperationCanceledException) when (cancellationToken.IsCancellationRequested)
    {
        // Log delay cancellation
        Debug.WriteLine("Delay was canceled");
        
        throw;
    }
}

My startup Configuration method:

public void Configuration(IAppBuilder app)
{
    var config = new HttpConfiguration();
    
    config.MapHttpAttributeRoutes();
    
    var builder = new ContainerBuilder();
    
    builder.RegisterApiControllers(Assembly.GetExecutingAssembly());

    builder.RegisterType<TestService>().As<ITestService>().InstancePerRequest();
    
    var container = builder.Build();
    config.DependencyResolver = new AutofacWebApiDependencyResolver(container);
    
    app.UseAutofacMiddleware(container);
    app.UseAutofacWebApi(config);
    app.UseWebApi(config);            
}

On the Network tab I see that the request was canceled: Network tab

My client code:

var xhttp = new XMLHttpRequest();
xhttp.open("GET", "http://localhost:59475/test/test");
...
xhttp.send();
setTimeout(() => xhttp.abort(), 5000)

I've tried running the above code on different projects and I've noticed an expected behavior, Web API, Web API with Autofac, Web API with Owin integration.

Upvotes: 0

Views: 38

Answers (0)

Related Questions