Sample: Task Parallel Library with CrmServiceClient

This C# .NET sample shows how to use the CrmServiceClient class with the Task Parallel Library (TPL).

Note

You could also use the ServiceClient instead of the CrmServiceClient class in this sample.

The Task Parallel Library enables developers to be more productive by simplifying the process of adding parallelism and concurrency to applications. Adding parallelism and concurrency can significantly improve the total throughput for applications that need to perform a large number of Dataverse operations in a short period of time.

Requirements

  • Visual Studio 2019 or later
  • Dataverse test environment and valid user logon credentials

How to run the sample

  1. Clone the PowerApps-Samples repository so that you have a copy locally.
  2. Open the dataverse\Xrm Tooling\TPLCrmServiceClient\TPLCrmServiceClient.sln file in Visual Studio.
  3. Press F5 to compile and run the program.

Demonstrates

Because the CrmServiceClient class includes handling for the transient errors thrown by the Dataverse service protection limits, the combination of TPL and CrmServiceClient is valuable to create applications that can optimize throughput while being resilient to the service protection limit errors by re-trying requests that are rejected due to these limits. The CrmServiceClient.Clone() method enables TPL to use the client with multiple threads.

More information: Service Protection API Limits

Note

You could also use the ServiceClient.Clone method in this sample code instead of CrmServiceClient.Clone() with the same results.

This sample will generate a number of account table rows using the Parallel.ForEach method, then it will use that technique again to delete the tables created. By default, this sample will create only 10 rows, which is not enough to hit the service protection API limit errors. If you raise the numberOfRecords variable value to 10000, you can use Fiddler to observe how some of the requests will be rejected and re-tried.

See also

Task Parallel Library (TPL)