MasterNeverDown.Retry 1.0.3

dotnet add package MasterNeverDown.Retry --version 1.0.3
                    
NuGet\Install-Package MasterNeverDown.Retry -Version 1.0.3
                    
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="MasterNeverDown.Retry" Version="1.0.3" />
                    
For projects that support PackageReference, copy this XML node into the project file to reference the package.
<PackageVersion Include="MasterNeverDown.Retry" Version="1.0.3" />
                    
Directory.Packages.props
<PackageReference Include="MasterNeverDown.Retry" />
                    
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 MasterNeverDown.Retry --version 1.0.3
                    
#r "nuget: MasterNeverDown.Retry, 1.0.3"
                    
#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 MasterNeverDown.Retry@1.0.3
                    
#: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=MasterNeverDown.Retry&version=1.0.3
                    
Install as a Cake Addin
#tool nuget:?package=MasterNeverDown.Retry&version=1.0.3
                    
Install as a Cake Tool

Vibe.Retry

一个轻量级的 .NET 重试库,支持可配置的延迟间隔和失败回调。

特性

  • 使用毫秒数组配置重试延迟间隔
  • 每次重试失败时的回调通知
  • 支持同步和异步操作
  • 基于 .NET 10.0 构建,使用最新 C# 特性
  • 零依赖,轻量高效

安装

dotnet add package Vibe.Retry

使用方法

异步操作重试

using Vibe.Retry;

// 带返回值的异步操作
var result = await Retry.ExecuteAsync(
    async () => await SomeApiCall(),
    retryDelaysMs: [100, 500, 1000, 5000],
    onFailure: ex => Console.WriteLine($"重试失败: {ex.Message}")
);

// 无返回值的异步操作
await Retry.ExecuteAsync(
    async () => await SendMessage(),
    retryDelaysMs: [200, 1000, 3000],
    onFailure: ex => LogError(ex)
);

同步操作重试

using Vibe.Retry;

// 带返回值的同步操作
var value = Retry.Execute(
    () => RiskyOperation(),
    retryDelaysMs: [50, 200, 500],
    onFailure: ex => LogError(ex)
);

// 无返回值的同步操作
Retry.Execute(
    () => SaveToFile(),
    retryDelaysMs: [100, 500],
    onFailure: ex => Console.WriteLine($"保存失败: {ex.Message}")
);

重试间隔说明

retryDelaysMs 数组定义了每次重试前的等待时间(毫秒):

// 示例:[100, 500, 1000]
// 首次执行 → 失败等待100ms → 第1次重试 → 失败等待500ms → 第2次重试 → 失败等待1000ms → 第3次重试 → 最终失败
int[] delays = [100, 500, 1000];

失败处理

当所有重试都失败时,会抛出 AggregateException,包含所有失败的异常:

try
{
    await Retry.ExecuteAsync(
        () => UnstableApi(),
        retryDelaysMs: [100, 500, 1000]
    );
}
catch (AggregateException ex)
{
    Console.WriteLine($"共失败 {ex.InnerExceptions.Count} 次");
    foreach (var inner in ex.InnerExceptions)
    {
        Console.WriteLine($"错误: {inner.Message}");
    }
}

API 参考

异步方法

Task<TResult> ExecuteAsync<TResult>(
    Func<Task<TResult>> operation,
    int[] retryDelaysMs,
    Action<Exception>? onFailure = null)

Task ExecuteAsync(
    Func<Task> operation,
    int[] retryDelaysMs,
    Action<Exception>? onFailure = null)

同步方法

TResult Execute<TResult>(
    Func<TResult> operation,
    int[] retryDelaysMs,
    Action<Exception>? onFailure = null)

void Execute(
    Action operation,
    int[] retryDelaysMs,
    Action<Exception>? onFailure = null)

许可证

MIT License

Product Compatible and additional computed target framework versions.
.NET net10.0 is compatible.  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.
  • net10.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.3 97 5/12/2026