Skip to content

Asynchronous action execution

reisenberger edited this page Feb 8, 2017 · 14 revisions

Asynchronous action execution

For an overview of syntax and policies available, read first the readme: https://github.com/App-vNext/Polly#asynchronous-support. This article describes the asynchronous operation of policies in detail.

Asynchronous action execution is available for all platform targets.

Async execution of actions through policies

In general, asynchronous policies execute actions in the same manner as synchronous policies. The following are the general differences:

Async / await

In asynchronous execution, all delegates are async and run with await.

Async synchronization context

As recommended for libraries offering an async API, by default, async continuations (when a method resumes from an await) are not run on a captured synchronization context. If you require the whole .ExecuteAsync(…) call to continue on the captured synchronization context, use the overloads taking a bool continueOnCapturedContext, and set this to true.

Cancellation support

Using an .ExecuteAsync(…) (or similar) method taking a CancellationToken allows async action execution to be cancelled. Cancellation can occur:

  • Before the delegate is actioned at all: In common with the Base Class Library implementation in Task.Run(…) and elsewhere, if the cancellation token is cancelled before execution begins, the delegate is not executed at all.
  • During delegate execution: The action delegates taken by the relevant .ExecuteAsync(…) overloads take a CancellationToken input parameter. The CancellationToken passed into .ExecuteAsync(…) call is passed in turn as the CancellationToken input parameter to the executed delegate, to support cancellation during delegate execution.
  • During any wait operations policies carry out: for example, waits between retries; waits for a bulkhead execution slot.

All cancellation throws the usual OperationCanceledException.

Clone this wiki locally