DmitryLegostaev.Polly.ConditionalWait 2.0.0

dotnet add package DmitryLegostaev.Polly.ConditionalWait --version 2.0.0
NuGet\Install-Package DmitryLegostaev.Polly.ConditionalWait -Version 2.0.0
This command is intended to be used within the Package Manager Console in Visual Studio, as it uses the NuGet module's version of Install-Package.
<PackageReference Include="DmitryLegostaev.Polly.ConditionalWait" Version="2.0.0" />
For projects that support PackageReference, copy this XML node into the project file to reference the package.
paket add DmitryLegostaev.Polly.ConditionalWait --version 2.0.0
#r "nuget: DmitryLegostaev.Polly.ConditionalWait, 2.0.0"
#r directive can be used in F# Interactive and Polyglot Notebooks. Copy this into the interactive tool or source code of the script to reference the package.
// Install DmitryLegostaev.Polly.ConditionalWait as a Cake Addin
#addin nuget:?package=DmitryLegostaev.Polly.ConditionalWait&version=2.0.0

// Install DmitryLegostaev.Polly.ConditionalWait as a Cake Tool
#tool nuget:?package=DmitryLegostaev.Polly.ConditionalWait&version=2.0.0

Nuget Build GitHub repo size

DmitryLegostaev.Polly.ConditionalWait

A small class library to provide Conditional Wait functionality using Polly v7 library.

Usage

Obtaining ConditionalWait object instance

Explicitly create ConditionalWait object

var conditionalWait = new ConditionalWait();

or use a DI to obtain ConditionalWait object.

(optional) Configuration of default Timeout and BackOffDelay

You can configure default timeout and backoffdelay for ConditionalWait object through its constructor. Defaults are 30s/300ms

var conditionalWait = new ConditionalWait(TimeSpan.FromSeconds(10), TimeSpan.FromSeconds(3));

or set environment variables

Environment.SetEnvironmentVariable("ConditionalWait__defaultTimeout", TimeSpan.FromSeconds(10).ToString());
Environment.SetEnvironmentVariable("ConditionalWait__defaultBackOffDelay", TimeSpan.FromSeconds(3).ToString());

Configuration priority is: Constructor parameters → Environment variables → Pre-defined defaults (30s/300ms)

Actual usage

Each method require Func to execute within its body. WaitForPredicateAndGetResult requires a predicate to be passed as argument in addition to Func.

// Wait for true to be returned from Func execution.
conditionalWait.WaitForTrue(() => 2 + 2 == 4);

// Wait for not null object to be returned from Func execution. Returns Func execution result to the calling method.
var equationResult = conditionalWait.WaitForAndGetResult(() => 2 + 2);

// Executing Func, obtaining its result, passing the result to the predicate, waiting for true to be returned from predicate. Returns Func execution result to the calling method.
var equationResult = conditionalWait.WaitForPredicateAndGetResult(() => 2 + 2, eq => eq == 4);

Each method could consume IConditionalWaitConfiguration object or Timeout/BackOffDelay. If 2nd option is chosen but Timeout or BackOffDelay is missing, then it will be obtained from ConditionalWait defaults defined during ConditionalWait object instantiation.

conditionalWait.WaitForTrue(() => 2 + 2 == 4,
    new ConditionalWaitConfiguration(TimeSpan.FromSeconds(10), TimeSpan.FromSeconds(3)));

conditionalWait.WaitForTrue(() => 2 + 2 == 4,
    TimeSpan.FromSeconds(10), TimeSpan.FromSeconds(3));
    
conditionalWait.WaitForTrue(() => 2 + 2 == 4,
    timeout: TimeSpan.FromSeconds(10));
    
conditionalWait.WaitForTrue(() => 2 + 2 == 4,
    backoffDelay: TimeSpan.FromSeconds(3));

Also ConditionalWait methods consume some optional arguments:

Argument name Type Purpose
exceptionsToIgnore IList<Type> List with Exception types (derived from System.Exception) to be ignored during ConditionalWait execution
failReason string String to be added to ConditionalWait timed out exception message
codePurpose string String to be added to each retry attempt message (doesn't work without logger)
logger ILogger Microsoft.Extensions.Logging object to add debug outputs during ConditionalWait execution
ConditionalWaitConfiguration

To create ConditionalWaitConfiguration object you should pass TimeSpan Timeout and TimeSpan BackOffDelay to its constructor. Also you can set Factor and BackoffType to customize main ConditionalWait behaviour based on WaitAndRetry policy.

ConditionalWaitConfiguration could be mapped from .NET Configuration by Microsoft.Extensions.Configuration.ConfigurationBinder.

To understand more about ConditionalWaitConfiguration capabilities visit https://github.com/Polly-Contrib/Polly.Contrib.WaitAndRetry

Product Compatible and additional computed target framework versions.
.NET net6.0 is compatible.  net6.0-android was computed.  net6.0-ios was computed.  net6.0-maccatalyst was computed.  net6.0-macos was computed.  net6.0-tvos was computed.  net6.0-windows was computed.  net7.0 was computed.  net7.0-android was computed.  net7.0-ios was computed.  net7.0-maccatalyst was computed.  net7.0-macos was computed.  net7.0-tvos was computed.  net7.0-windows was computed.  net8.0 was computed.  net8.0-android was computed.  net8.0-browser was computed.  net8.0-ios was computed.  net8.0-maccatalyst was computed.  net8.0-macos was computed.  net8.0-tvos was computed.  net8.0-windows was computed. 
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.

NuGet packages (1)

Showing the top 1 NuGet packages that depend on DmitryLegostaev.Polly.ConditionalWait:

Package Downloads
DmitryLegostaev.Polly.ActionRetry

A small class library to provide Action Retry functionality using Polly v7 library.

GitHub repositories

This package is not used by any popular GitHub repositories.

Version Downloads Last updated
2.0.0 189 7/23/2023
1.0.0 137 6/30/2023