Dependency and execution order in asynchronous operations

 

Applies To: Dynamics CRM 2015

An asynchronous operation can be made dependent with another asynchronous operation. A dependent asynchronous operation does not execute until the operation that it is dependent with has finished executing. For example, if asynchronous operation B is dependent with asynchronous operation A, operation B does not execute until after operation A executes and is completed. This dependency between asynchronous operations is established by setting the AsyncOperation.DependencyToken attribute, which can contain any string value. Any dependent asynchronous operations must have the same AsyncOperation.DependencyToken value.

The Microsoft Dynamics CRM platform establishes which asynchronous operation is dependent when the operations are created in the asynchronous queue. For example, consider three asynchronous operations named A, B, and C, where B and C should only execute after A is completed. To establish the correct dependencies, A must be created in the queue first, followed by B and then C. This is because the order in which the operations are created establishes the sequence of their execution. In this example, all three asynchronous operations have the same AsyncOperation.DependencyToken. At run time, A executes first. After A has completed, B executes to completion, and then C executes until completion.

In the previous example, if operation B is postponed, operation A executes until it is completed and operation C is blocked until operation B is no longer postponed and executes until completed. You can postpone execution of an asynchronous operation by using the AsyncOperation.PostponeUntil attribute.

Any asynchronous operation that has a AsyncOperation.DependencyToken value of null executes independent of all other asynchronous operations in the queue. The order of execution with regard to other independent operations is not guaranteed. However, asynchronous operations created earlier have a better chance of executing before operations created later.  This assumes that the operations are not postponed and are not set to a state of Completed.

The dependency token must be set when the asynchronous operation is created. Because Microsoft Dynamics CRM creates asynchronous operations for bulk operations such as bulk email, bulk delete, and import, you cannot make use of the dependency token for these operations. In addition, the dependency token cannot be used to order execution of asynchronous registered plug-ins because the asynchronous operation that executes plug-ins is created by the Queue Manager. For more information, refer to Asynchronous service architecture.

See Also

Asynchronous service in Microsoft Dynamics CRM
Recurrence pattern in asynchronous job execution

© 2016 Microsoft. All rights reserved. Copyright