Reputation: 1
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:
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:
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