Search code examples
c#asp.net-corerazor-pagescancellation-token

Cancel long running operation on client cancel


I started my first project with asp.net core and razor pages. On a client request, a long running database operation will be started. Now i want to recognize, when the user leaves the website, so the database operation can be cancelled.

I've tried it with a cancellationToken, but it will never be cancelled.

public async Task<JsonResult> OnPostReadAsync([DataSourceRequest] DataSourceRequest request, CancellationToken cancellationToken)
{
    var messages = await _logMessageService.GetLogMessagesAsync(request, cancellationToken);

    return new JsonResult(messages.ToDataSourceResult(request));
}

The function get called by an Telerik Kendo UI Grid. Can you tell me, why the cancellation token don't get cancelled, or what other options I have to detect a abortion by the client?

Edit 1

I pass the Token through to this function call of a NpgsqlCommand:

var dataReader = await command.ExecuteReaderAsync(cancellationToken);

Solution

  • So I found the answer myself after some more research. The problem is a bug in IISExpress as mentioned here: https://github.com/aspnet/Mvc/issues/5239#issuecomment-323567952

    I switched to Kestrel and now everything works as expected.