# Class RetryExtensions OptimaJet.Workflow.Core.RetryExtensions Provides extension methods for retrying asynchronous operations created by task factories. Namespace: [OptimaJet.Workflow.Core](https://api.workflowengine.io/core/OptimaJet.Workflow.Core/) ```csharp public static class RetryExtensions ``` ## Methods `public static Task RetryAsync(this Func> taskFactory, int retries, TimeSpan delay, CancellationToken cancellationToken = default, Func isRetryAllowed = null, bool progressiveDelay = true)` Executes the asynchronous operation produced by `taskFactory` and retries it when it faults. ### Parameters | Name | Type | Description | | --- | --- | --- | | `taskFactory` | `Func>` | A delegate that creates a new [Task](https://learn.microsoft.com/dotnet/api/system.threading.tasks.task-1) for each retry attempt. | | `retries` | `Int32` | The number of additional attempts allowed after the first failed attempt. | | `delay` | `TimeSpan` | The delay before the first retry attempt. | | `cancellationToken` | `CancellationToken` | A token observed after each attempt and while waiting between attempts. It is not passed to `taskFactory`. | | `isRetryAllowed` | `Func` | Optional predicate that decides whether a failed attempt can be retried. The predicate receives the [AggregateException](https://learn.microsoft.com/dotnet/api/system.aggregateexception) from the failed attempt. | | `progressiveDelay` | `Boolean` | [true](https://learn.microsoft.com/dotnet/csharp/language-reference/builtin-types/bool) to set the next delay to [TimeSpan.FromTicks(long)](https://learn.microsoft.com/dotnet/api/system.timespan.fromticks)(2 \* `delay`.Ticks); [false](https://learn.microsoft.com/dotnet/csharp/language-reference/builtin-types/bool) to reuse `delay`. | ### Returns | Type | Description | | --- | --- | | `Task` | The value returned by a successful operation attempt. | ### Exceptions | Type | Description | | --- | --- | | `AggregateException` | Thrown when a fault is not eligible for another attempt or the retry limit is reached. | | `OperationCanceledException` | Thrown when `cancellationToken` is canceled. | `public static Task RetryWithDefaultRetryAllowedChecker(this Func> taskFactory, int retries, TimeSpan delay, CancellationToken cancellationToken = default)` Executes the asynchronous operation produced by `taskFactory` and retries it when it faults. ### Parameters | Name | Type | Description | | --- | --- | --- | | `taskFactory` | `Func>` | A delegate that creates a new [Task](https://learn.microsoft.com/dotnet/api/system.threading.tasks.task-1) for each retry attempt. | | `retries` | `Int32` | The number of additional attempts allowed after the first failed attempt. | | `delay` | `TimeSpan` | The delay before the first retry attempt. | | `cancellationToken` | `CancellationToken` | A token observed after each attempt and while waiting between attempts. It is not passed to `taskFactory`. | ### Returns | Type | Description | | --- | --- | | `Task` | A task that completes with the successful result of `taskFactory` or fails when retries are exhausted. | ### Remarks Retries only when the exception itself or its [Exception.InnerException](https://learn.microsoft.com/dotnet/api/system.exception.innerexception) chain contains a [PersistenceProviderQueryException](https://api.workflowengine.io/core/OptimaJet.Workflow.Core.Fault/PersistenceProviderQueryException/) whose [PersistenceProviderQueryException.IsRetryAllowed](https://api.workflowengine.io/core/OptimaJet.Workflow.Core.Fault/PersistenceProviderQueryException/) property is [true](https://learn.microsoft.com/dotnet/csharp/language-reference/builtin-types/bool). `public static Task RetryAsync(this Func taskFactory, int retries, TimeSpan delay, CancellationToken cancellationToken = default, Func isRetryAllowed = null, bool progressiveDelay = true)` Executes the asynchronous operation produced by `taskFactory` and retries it when it faults. ### Parameters | Name | Type | Description | | --- | --- | --- | | `taskFactory` | `Func` | A delegate that creates a new [Task](https://learn.microsoft.com/dotnet/api/system.threading.tasks.task) for each retry attempt. | | `retries` | `Int32` | The number of additional attempts allowed after the first failed attempt. | | `delay` | `TimeSpan` | The delay before the first retry attempt. | | `cancellationToken` | `CancellationToken` | A token observed after each attempt and while waiting between attempts. It is not passed to `taskFactory`. | | `isRetryAllowed` | `Func` | Optional predicate that decides whether a failed attempt can be retried. The predicate receives the [AggregateException](https://learn.microsoft.com/dotnet/api/system.aggregateexception) from the failed attempt. | | `progressiveDelay` | `Boolean` | [true](https://learn.microsoft.com/dotnet/csharp/language-reference/builtin-types/bool) to set the next delay from twice the [TimeSpan.Milliseconds](https://learn.microsoft.com/dotnet/api/system.timespan.milliseconds) component of `delay`; [false](https://learn.microsoft.com/dotnet/csharp/language-reference/builtin-types/bool) to reuse `delay`. | ### Returns | Type | Description | | --- | --- | | `Task` | A task that represents the full retry sequence. | ### Exceptions | Type | Description | | --- | --- | | `AggregateException` | Thrown when a fault is not eligible for another attempt or the retry limit is reached. | | `OperationCanceledException` | Thrown when `cancellationToken` is canceled. | `public static Task RetryWithDefaultRetryAllowedChecker(this Func taskFactory, int retries, TimeSpan delay, CancellationToken cancellationToken = default)` Executes the asynchronous operation produced by `taskFactory` and retries it when it faults. ### Parameters | Name | Type | Description | | --- | --- | --- | | `taskFactory` | `Func` | A delegate that creates a new [Task](https://learn.microsoft.com/dotnet/api/system.threading.tasks.task) for each retry attempt. | | `retries` | `Int32` | The number of additional attempts allowed after the first failed attempt. | | `delay` | `TimeSpan` | The delay before the first retry attempt. | | `cancellationToken` | `CancellationToken` | A token observed after each attempt and while waiting between attempts. It is not passed to `taskFactory`. | ### Returns | Type | Description | | --- | --- | | `Task` | A task that represents the retry sequence. | ### Remarks Retries only when the exception itself or its [Exception.InnerException](https://learn.microsoft.com/dotnet/api/system.exception.innerexception) chain contains a [PersistenceProviderQueryException](https://api.workflowengine.io/core/OptimaJet.Workflow.Core.Fault/PersistenceProviderQueryException/) whose [PersistenceProviderQueryException.IsRetryAllowed](https://api.workflowengine.io/core/OptimaJet.Workflow.Core.Fault/PersistenceProviderQueryException/) property is [true](https://learn.microsoft.com/dotnet/csharp/language-reference/builtin-types/bool). [Previous PersistenceProviderId](https://api.workflowengine.io/core/OptimaJet.Workflow.Core/PersistenceProviderId/) [Next RSACryptoServiceProviderExtensions](https://api.workflowengine.io/core/OptimaJet.Workflow.Core/RSACryptoServiceProviderExtensions/)