-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathHttpRetryAttribute.cs
More file actions
41 lines (38 loc) · 1.42 KB
/
HttpRetryAttribute.cs
File metadata and controls
41 lines (38 loc) · 1.42 KB
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
using System;
namespace ZibStack.NET.Aop;
/// <summary>
/// Built-in aspect that retries HTTP calls on transient failures using Polly.
/// Pre-configured to handle the same transient errors as
/// <c>Microsoft.Extensions.Http.Resilience</c>:
/// <list type="bullet">
/// <item><see cref="System.Net.Http.HttpRequestException"/></item>
/// <item><see cref="TaskCanceledException"/> (HTTP timeouts)</item>
/// <item>HTTP 408, 429, 500, 502, 503, 504 (when available via <c>HttpRequestException.StatusCode</c>)</item>
/// </list>
///
/// <para>
/// Requires the <c>Polly.Core</c> NuGet package.
/// </para>
/// </summary>
/// <example>
/// <code>
/// [HttpRetry]
/// public async Task<string> CallApiAsync(string url) { ... }
///
/// [HttpRetry(MaxRetryAttempts = 5, DelayMs = 500)]
/// public async Task<OrderResponse> PlaceOrderAsync(OrderRequest req) { ... }
/// </code>
/// </example>
[AspectHandler(typeof(PollyHttpRetryHandler))]
[AttributeUsage(AttributeTargets.Method | AttributeTargets.Class, AllowMultiple = false, Inherited = true)]
public sealed class PollyHttpRetryAttribute : AspectAttribute
{
/// <summary>
/// Maximum number of retry attempts (not counting the initial call). Default: 3.
/// </summary>
public int MaxRetryAttempts { get; set; } = 3;
/// <summary>
/// Base delay in milliseconds between retries. Default: 200.
/// </summary>
public int DelayMs { get; set; } = 200;
}