1
+ using System . Net ;
1
2
using System . Net . Http . Headers ;
2
3
using Microsoft . Extensions . DependencyInjection ;
4
+ using Polly ;
5
+ using Polly . Extensions . Http ;
3
6
4
7
namespace Cnblogs . Architecture . Ddd . Cqrs . ServiceAgent ;
5
8
@@ -13,38 +16,53 @@ public static class InjectExtensions
13
16
/// </summary>
14
17
/// <param name="services">The <see cref="IServiceCollection"/>.</param>
15
18
/// <param name="baseUri">The base uri for api.</param>
16
- /// <typeparam name="T">The type of service agent</typeparam>
19
+ /// <param name="policy">The polly policy for underlying httpclient.</param>
20
+ /// <typeparam name="TClient">The type of service agent</typeparam>
17
21
/// <returns></returns>
18
- public static IHttpClientBuilder AddServiceAgent < T > ( this IServiceCollection services , string baseUri )
19
- where T : CqrsServiceAgent
22
+ public static IHttpClientBuilder AddServiceAgent < TClient > (
23
+ this IServiceCollection services ,
24
+ string baseUri ,
25
+ IAsyncPolicy < HttpResponseMessage > ? policy = null )
26
+ where TClient : class
20
27
{
21
- return services . AddHttpClient < T > (
28
+ policy ??= GetDefaultPolicy ( ) ;
29
+ return services . AddHttpClient < TClient > (
22
30
h =>
23
31
{
24
32
h . BaseAddress = new Uri ( baseUri ) ;
25
33
h . DefaultRequestHeaders . Accept . Add ( new MediaTypeWithQualityHeaderValue ( "application/cqrs" ) ) ;
26
- } ) ;
34
+ } ) . AddPolicyHandler ( policy ) ;
27
35
}
28
36
29
37
/// <summary>
30
38
/// Inject a service agent to services.
31
39
/// </summary>
32
40
/// <param name="services">The <see cref="IServiceCollection"/>.</param>
33
41
/// <param name="baseUri">The base uri for api.</param>
42
+ /// <param name="policy">The polly policy for underlying httpclient.</param>
34
43
/// <typeparam name="TClient">The type of api client.</typeparam>
35
44
/// <typeparam name="TImplementation">The type of service agent</typeparam>
36
45
/// <returns></returns>
37
46
public static IHttpClientBuilder AddServiceAgent < TClient , TImplementation > (
38
47
this IServiceCollection services ,
39
- string baseUri )
48
+ string baseUri ,
49
+ IAsyncPolicy < HttpResponseMessage > ? policy = null )
40
50
where TClient : class
41
- where TImplementation : CqrsServiceAgent , TClient
51
+ where TImplementation : class , TClient
42
52
{
53
+ policy ??= GetDefaultPolicy ( ) ;
43
54
return services . AddHttpClient < TClient , TImplementation > (
44
55
h =>
45
56
{
46
57
h . BaseAddress = new Uri ( baseUri ) ;
47
58
h . DefaultRequestHeaders . Accept . Add ( new MediaTypeWithQualityHeaderValue ( "application/cqrs" ) ) ;
48
- } ) ;
59
+ } ) . AddPolicyHandler ( policy ) ;
60
+ }
61
+
62
+ private static IAsyncPolicy < HttpResponseMessage > GetDefaultPolicy ( )
63
+ {
64
+ return HttpPolicyExtensions . HandleTransientHttpError ( )
65
+ . OrResult ( msg => msg . StatusCode == HttpStatusCode . TooManyRequests )
66
+ . WaitAndRetryAsync ( 3 , _ => TimeSpan . FromMilliseconds ( 1500 ) ) ;
49
67
}
50
68
}
0 commit comments