sim756.RetryEx 1.0.0

dotnet add package sim756.RetryEx --version 1.0.0
                    
NuGet\Install-Package sim756.RetryEx -Version 1.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="sim756.RetryEx" Version="1.0.0" />
                    
For projects that support PackageReference, copy this XML node into the project file to reference the package.
<PackageVersion Include="sim756.RetryEx" Version="1.0.0" />
                    
Directory.Packages.props
<PackageReference Include="sim756.RetryEx" />
                    
Project file
For projects that support Central Package Management (CPM), copy this XML node into the solution Directory.Packages.props file to version the package.
paket add sim756.RetryEx --version 1.0.0
                    
#r "nuget: sim756.RetryEx, 1.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.
#:package sim756.RetryEx@1.0.0
                    
#:package directive can be used in C# file-based apps starting in .NET 10 preview 4. Copy this into a .cs file before any lines of code to reference the package.
#addin nuget:?package=sim756.RetryEx&version=1.0.0
                    
Install as a Cake Addin
#tool nuget:?package=sim756.RetryEx&version=1.0.0
                    
Install as a Cake Tool

Resilient Retry Mechanism

Examples

Example 1

Retry with Exception handling and executing only on Invoke() call.

Retry numberRetry = new Retry(
    retryAction: () =>
    {
        WriteExampleHeader();

        int randomNumber = GenerateRandomNumber();
        Console.WriteLine($"Random number: {randomNumber}");
    },
    exceptionAction: (exception) =>
    {
        Console.WriteLine($"Exception: {exception.Message}");
    }
);

numberRetry.Invoke();

Example 2

Retry with Exception handling with specified delays and retry count; no manual invocation needed, it auto executes.

new Retry
(
    retryAction: () =>
    {
        WriteExampleHeader();
        
        int randomNumber = GenerateRandomNumber();
        Console.WriteLine($"Random number: {randomNumber}");
    },
    count: 10, // retying 10 times until success or all 10 retries fails.
    delay: 1000, // 1-second delay between retries.
    exceptionAction: (exception) =>
    {
        Console.WriteLine($"Exception: {exception.Message}");
    }
).Invoke();

Example 3

Retry with parameters and return value.

int retValExample3 = new Retry<(int para1, int para2, int para3), int>
(
    param =>
    {
        WriteExampleHeader();

        int randomNumber = GenerateRandomNumber();
        int result = randomNumber + param.para1 + param.para2 + param.para3;
        return result; // Returning result to the retVal.
    },
    (10, 100, 1000), // parameter values
    2, // retry count
    1000, // delay in milliseconds
    ex =>
    {
        Console.WriteLine($"Exception: {ex.Message}");
    }
).Invoke();

Console.WriteLine($"retValExample3: {retValExample3}");

Example 4

Retry with parameters and return value.

Retry<(int para1, int para2, int para3), int> retryExample4
    = new
    (
        param =>
        {
            WriteExampleHeader();
            
            int randomNumber = GenerateRandomNumber();
            int result = randomNumber + param.para1 + param.para2 + param.para3;
            return result; // Returning result to the retVal.
        },
        (10, 100, 1000), // parameter values
        count: 2, // retry count
        delay: 1000, // delay in milliseconds
        ex =>
        {
            Console.WriteLine($"Exception: {ex.Message}"); 
        }
    );

retryExample4.Invoke();

Console.WriteLine($"retryExample4.Result: {retryExample4.Result}");

Example 5

Retry with parameters and return value, and with additional custom exception handling.

Retry<(int para1, int para2, int para3), int> retryExample5
    = new(
        param =>
        {
            WriteExampleHeader();

            int randomNumber = GenerateRandomNumber();
            int result = randomNumber + param.para1 + param.para2 + param.para3;
            return result; // Returning result to the retVal.
        },
        (10, 100, 1000), // parameter values
        2, // retry count
        1000, // delay in milliseconds
        exception =>
        {
            try
            {
                throw exception;
            }
            catch (InvalidDataException ex)
            {
                Console.WriteLine($"Exception: {ex.Message}");
            }
            catch (Exception e)
            {
                Console.WriteLine(e.StackTrace);
            }
        }
    );

retryExample5.Invoke();
Console.WriteLine($"retryExample5.Result: {retryExample5.Result}");
Product Compatible and additional computed target framework versions.
.NET net9.0 is compatible.  net9.0-android was computed.  net9.0-browser was computed.  net9.0-ios was computed.  net9.0-maccatalyst was computed.  net9.0-macos was computed.  net9.0-tvos was computed.  net9.0-windows was computed.  net10.0 was computed.  net10.0-android was computed.  net10.0-browser was computed.  net10.0-ios was computed.  net10.0-maccatalyst was computed.  net10.0-macos was computed.  net10.0-tvos was computed.  net10.0-windows was computed. 
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.
  • net9.0

    • No dependencies.

NuGet packages

This package is not used by any NuGet packages.

GitHub repositories

This package is not used by any popular GitHub repositories.

Version Downloads Last Updated
1.0.0 152 5/21/2025

Resilient retry mechanism