As you stated that your async controller expects a Task that sometimes gets some kind of exception, I offer you the ContinueWith extension method for a task that can only be started when your task crashes, for example this:
task.ContinueWith( t => logger.Error(t.Exception.Message, t.Exception); , TaskContinuationOptions.OnlyOnFaulted);
This is the default mechanism for handling exceptions, and it will work in an OWIN application.
Secondly, with regard to cancellation: the task can be started using the CancellationToken structure, which can be used to cancel the task at run time. You can read more in the MSDN article .
HttpResponse.ClientDisconnectedToken used for situations when the client has been disconnected and the request should not be executed at execution.
You can use this token or create your own CancellationTokenSource , for example:
var source = new CancellationTokenSource(); var token = source.Token; var task = Task.Factory.StartNew(() => { // Were we already canceled? ct.ThrowIfCancellationRequested(); var moreToDo = true; while (moreToDo) { // Poll on this property if you have to do // other cleanup before throwing. if (ct.IsCancellationRequested) { // Clean up here, then... ct.ThrowIfCancellationRequested(); } } }, token);
VMAtm source share