-
Notifications
You must be signed in to change notification settings - Fork 447
/
Copy pathTaskExtenstions.cs
92 lines (86 loc) · 3.46 KB
/
TaskExtenstions.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
using System;
using WebApiClientCore.Implementations.Tasks;
namespace WebApiClientCore
{
/// <summary>
/// ITask扩展
/// </summary>
public static class TaskExtenstions
{
/// <summary>
/// 返回提供请求重试的请求任务对象
/// </summary>
/// <typeparam name="TResult"></typeparam>
/// <param name="task"></param>
/// <param name="maxCount">最大重试次数</param>
/// <exception cref="ArgumentOutOfRangeException"></exception>
/// <exception cref="ArgumentNullException"></exception>
/// <returns></returns>
public static IRetryTask<TResult> Retry<TResult>(this ITask<TResult> task, int maxCount)
{
return task.Retry(maxCount, null);
}
/// <summary>
/// 返回提供请求重试的请求任务对象
/// </summary>
/// <typeparam name="TResult"></typeparam>
/// <param name="task"></param>
/// <param name="maxCount">最大重试次数</param>
/// <param name="delay">各次重试的延时时间</param>
/// <exception cref="ArgumentOutOfRangeException"></exception>
/// <exception cref="ArgumentNullException"></exception>
/// <returns></returns>
public static IRetryTask<TResult> Retry<TResult>(this ITask<TResult> task, int maxCount, TimeSpan delay)
{
return task.Retry(maxCount, (i) => delay);
}
/// <summary>
/// 返回提供请求重试的请求任务对象
/// </summary>
/// <typeparam name="TResult"></typeparam>
/// <param name="task"></param>
/// <param name="maxCount">最大重试次数</param>
/// <param name="delay">各次重试的延时时间</param>
/// <exception cref="ArgumentOutOfRangeException"></exception>
/// <exception cref="ArgumentNullException"></exception>
/// <returns></returns>
public static IRetryTask<TResult> Retry<TResult>(this ITask<TResult> task, int maxCount, Func<int, TimeSpan>? delay)
{
if (task == null)
{
throw new ArgumentNullException(nameof(task));
}
if (maxCount < 1)
{
throw new ArgumentOutOfRangeException(nameof(maxCount));
}
return new ActionRetryTask<TResult>(async () => await task, maxCount, delay);
}
/// <summary>
/// 当遇到异常时返回默认值
/// </summary>
/// <typeparam name="TResult"></typeparam>
/// <param name="task"></param>
/// <exception cref="ArgumentNullException"></exception>
/// <returns></returns>
public static ITask<TResult> HandleAsDefaultWhenException<TResult>(this ITask<TResult> task)
{
#nullable disable
return task.Handle().WhenCatch<Exception>(ex => default);
#nullable enable
}
/// <summary>
/// 返回提供异常处理请求任务对象
/// </summary>
/// <typeparam name="TResult"></typeparam>
/// <param name="task"></param>
/// <exception cref="ArgumentNullException"></exception>
/// <returns></returns>
public static IHandleTask<TResult> Handle<TResult>(this ITask<TResult> task)
{
return task == null
? throw new ArgumentNullException(nameof(task))
: (IHandleTask<TResult>)new ActionHandleTask<TResult>(async () => await task);
}
}
}