Higher thread usage in .NET 4.7.2

  • Description

    Calling an async code in a synchronous execution path leads to the calling thread being paused until the async part finishes (the corresponding Task object is marked as completed). Because of the specific implementation of async methods in C#,  the Task object cannot be marked as complete until continuations are executed. In most of the scenarios, continuations are scheduled to be executed on thread pool threads. Therefore, the calling thread starts to depend on the availability of worker threads in the default thread pool.

    This mostly means that calling an async API in a synchronous context needs two threads to do the job. An application must always have free threads, otherwise an endless blocking can occur due to a lack of worker threads to call continuation and unblock main thread. This design constraint could be resolved by using a custom thread scheduler that can take advantage of a blocked thread to execute the operation, thereby eliminating the need for an extra thread.

    Sitecore xConnect is written according to high-performance application best practices, with optimized thread consumption. Its design allows for taking full advantage of async operation processing and it defines the custom scheduler to tackle excessive thread consumption.

    Unfortunately, the recent changes in .NET Framework 4.7.2 (System.Net.Http assembly) removed the respect of custom thread schedulers, which has led to excessive thread consumption by sync-async operations.

  • You can configure the Sitecore application to use the previous version of System.Net.Http.dll instead of the .NET Framework 4.7.2 version.

    If you have a custom solution in Visual Studio configured to target .NET Framework 4.7.1, then Visual Studio references System.Net.Http.dll of version 4.2.0.0. If you copy this dll to the \bin folder, you can force Sitecore XP to use it instead of the library from GAC by doing the following:

    1. Remove all existing binding redirects for System.Net.Http from the Web.config file.
    2. Add the following binding redirect to the Web.config file:
      <dependentAssembly>
        <assemblyIdentity name="System.Net.Http" publicKeyToken="b03f5f7f11d50a3a" xmlns="urn:schemas-microsoft-com:asm.v1" />
        <bindingRedirect oldVersion="0.0.0.0-4.2.0.0" newVersion="4.2.0.0" xmlns="urn:schemas-microsoft-com:asm.v1" />
      </dependentAssembly>

    When version 4.2.0.0 of System.Net.Http library is used, it contains an implementation of the HttpClient class that respects custom schedulers. Therefore, synchronous methods of xConnect client require just a single thread to complete a request.

Applies to:

CMS 9.0 Initial Release+

February 04, 2019
February 04, 2019

Reference number:

252894