AsyncLazy<T>.SuppressRecursiveFactoryDetection Property
Definition
Important
Some information relates to prerelease product that may be substantially modified before it’s released. Microsoft makes no warranties, express or implied, with respect to the information provided here.
Gets a value indicating whether to suppress detection of a value factory depending on itself.
public bool SuppressRecursiveFactoryDetection { get; init; }
member this.SuppressRecursiveFactoryDetection : bool with get, set
Public Property SuppressRecursiveFactoryDetection As Boolean
Property Value
The default value is false
.
Remarks
A value factory that truly depends on itself (e.g. by calling GetValueAsync() on the same instance) would deadlock, and by default this class will throw an exception if it detects such a condition. However this detection relies on the .NET ExecutionContext, which can flow to "spin off" contexts that are not awaited by the factory, and thus could legally await the result of the value factory without deadlocking.
When this flows improperly, it can cause InvalidOperationException to be thrown, but only when the value factory has not already been completed, leading to a difficult to reproduce race condition. Such a case can be resolved by calling SuppressRelevance() around the non-awaited fork in ExecutionContext, or the entire instance can be configured to suppress this check by setting this property to true
.
When this property is set to true
, the recursive factory check will not be performed, but SuppressRelevance() will still call into SuppressRelevance() if a JoinableTaskFactory was provided to the constructor.