Use ExecuteAsync to execute messages asynchronously

Except for a few operations described below, all data operations using the SDK assembly request classes are synchronous.

Importing a solution is one operation which can require considerable resources, so there is an option to execute this operation asynchronously using the ExecuteAsyncRequest request class. The DeleteAndPromoteRequest request class performs similar resource intensive operations.

Importing a solution asynchronously improves system performance by postponing message execution until some later time when the server load may be less. Interactive users do not have to wait for the target message to execute before they can continue. This is especially useful when importing solutions which may take a few minutes or more to execute.

Merging rows with a large number of related activities or other table types can also take a long time to update all the related rows, so executing these in the background can improve the user experience.

Note

ImportSolutionRequest, DeleteAndPromoteRequest, and MergeRequest are the only request classes that can be used with ExecuteAsyncRequest.

Use the ExecuteAsyncRequest request class to execute a message asynchronously. You configure the request and pass the request instance as an argument to IOrganizationService.Execute. ExecuteAsyncResponse returns with the ID of the asynchronous job. You can (optionally) query the job using the ID to find out its current state.

You can use the ExecuteMultipleRequest request class to queue multiple solutions to be imported asynchronously. To do this, add one or more ExecuteAsync message requests to an ExecuteMultiple message request. Due to throttling restrictions that improve overall system performance, only one message running asynchronously is allowed to execute at a time for each organization.

For more information about the ExecuteMultiple message, see Execute multiple requests using the SDK for .NET.

Example

The following example shows how to use the ExecuteAsyncRequest message request class with the ImportSolutionRequest message request class.

string ManagedSolutionLocation = @"C:\temp\ManagedSolutionForImportExample.zip";

byte[] fileBytes = File.ReadAllBytes(ManagedSolutionLocation);

ImportSolutionRequest impSolReq = new ImportSolutionRequest()
{
CustomizationFile = fileBytes
};

ExecuteAsyncRequest asyncReq = new ExecuteAsyncRequest()
{
Request = impSolReq
};

var asyncResp = (ExecuteAsyncResponse)svc.Execute(asyncReq);

Guid asyncOperationId = asyncResp.AsyncJobId;

You can then poll the AsyncOperation table using the asyncOperationId value for the system job with the matching AsyncOperationId to detect when the StatusCode value indicates whether the operation has succeeded (30), failed (31), or was cancelled (32).

See Also

Use messages with the SDK for .NET
Use ExecuteTransaction
Execute multiple requests using the SDK for .NET