|
11 | 11 |
|
12 | 12 | namespace RetryPatternSample
|
13 | 13 | {
|
14 |
| - public class Startup |
15 |
| - { |
16 |
| - public Startup(IConfiguration configuration) |
17 |
| - { |
18 |
| - Configuration = configuration; |
19 |
| - } |
20 |
| - |
21 |
| - private ILogger<Startup> _logger; |
22 |
| - |
23 |
| - public IConfiguration Configuration { get; } |
24 |
| - |
25 |
| - // This method gets called by the runtime. Use this method to add services to the container. |
26 |
| - public void ConfigureServices(IServiceCollection services) |
27 |
| - { |
28 |
| - //429 - Throttling - retry twice, incrementing wait time in every retry. |
29 |
| - var retryWhenThrottling = Policy |
30 |
| - .HandleResult<HttpResponseMessage>(r => r.StatusCode == HttpStatusCode.TooManyRequests) |
31 |
| - .WaitAndRetryAsync(2, retryAttempt => TimeSpan.FromSeconds(Math.Pow(5, retryAttempt))); |
32 |
| - |
33 |
| - //408 - Timeout, retry twice, with a 5 secs wait time |
34 |
| - var retryWhenTimeout = Policy |
35 |
| - .HandleResult<HttpResponseMessage>(r => r.StatusCode == HttpStatusCode.RequestTimeout) |
36 |
| - .WaitAndRetryAsync(2, retryAttempt => TimeSpan.FromSeconds(5)); |
37 |
| - |
38 |
| - //503 or 5xx service unavailable - wait 10 secs and retry only once. |
39 |
| - var retryWhenServiceUnavailable = Policy |
40 |
| - .HandleResult<HttpResponseMessage>(r => r.StatusCode == HttpStatusCode.ServiceUnavailable) |
41 |
| - .WaitAndRetryAsync(1, retryAttempt => TimeSpan.FromSeconds(10)); |
42 |
| - |
43 |
| - //401 unauthorized - retry once and do some retry logic + logging |
44 |
| - var retryWhenUnauthorized = Policy |
45 |
| - .HandleResult<HttpResponseMessage>(r => r.StatusCode == HttpStatusCode.Unauthorized) |
46 |
| - .RetryAsync(1, (exception, retryCount) => |
47 |
| - { |
48 |
| - |
49 |
| - this._logger.LogError($"Error occurred retry attempt: {retryCount}, Error details: {exception.Result.ToString()}"); |
50 |
| - //Do some logic here like: |
51 |
| - //RenewAccessToken(); |
52 |
| - }); |
53 |
| - |
54 |
| - IAsyncPolicy<HttpResponseMessage> policyWrap = Policy.WrapAsync(retryWhenThrottling, retryWhenTimeout, retryWhenServiceUnavailable, retryWhenUnauthorized); |
55 |
| - |
56 |
| - services.AddHttpClient("SampleService", client => |
57 |
| - { |
58 |
| - client.BaseAddress = new Uri(@"<You endpoint's base address here>"); |
59 |
| - client.DefaultRequestHeaders.Add("Accept", "application/json"); |
60 |
| - }).AddPolicyHandler(policyWrap); |
61 |
| - |
62 |
| - services.AddControllers(); |
63 |
| - } |
64 |
| - |
65 |
| - // This method gets called by the runtime. Use this method to configure the HTTP request pipeline. |
66 |
| - public void Configure(IApplicationBuilder app, IWebHostEnvironment env, ILogger<Startup> logger) |
67 |
| - { |
68 |
| - this._logger = logger; |
69 |
| - |
70 |
| - if (env.IsDevelopment()) |
71 |
| - { |
72 |
| - app.UseDeveloperExceptionPage(); |
73 |
| - } |
74 |
| - |
75 |
| - app.UseHttpsRedirection(); |
76 |
| - |
77 |
| - app.UseRouting(); |
78 |
| - |
79 |
| - app.UseAuthorization(); |
80 |
| - |
81 |
| - app.UseEndpoints(endpoints => |
82 |
| - { |
83 |
| - endpoints.MapControllers(); |
84 |
| - }); |
85 |
| - } |
86 |
| - } |
| 14 | + public class Startup |
| 15 | + { |
| 16 | + public Startup(IConfiguration configuration) |
| 17 | + { |
| 18 | + Configuration = configuration; |
| 19 | + } |
| 20 | + |
| 21 | + private ILogger<Startup> _logger; |
| 22 | + |
| 23 | + public IConfiguration Configuration { get; } |
| 24 | + |
| 25 | + // This method gets called by the runtime. Use this method to add services to the container. |
| 26 | + public void ConfigureServices(IServiceCollection services) |
| 27 | + { |
| 28 | + //429 - Throttling - retry twice, incrementing wait time in every retry. |
| 29 | + var retryWhenThrottling = Policy |
| 30 | + .HandleResult<HttpResponseMessage>(r => r.StatusCode == HttpStatusCode.TooManyRequests) |
| 31 | + .WaitAndRetryAsync(2, retryAttempt => TimeSpan.FromSeconds(Math.Pow(5, retryAttempt))); |
| 32 | + |
| 33 | + //408 - Timeout, retry twice, with a 5 secs wait time |
| 34 | + var retryWhenTimeout = Policy |
| 35 | + .HandleResult<HttpResponseMessage>(r => r.StatusCode == HttpStatusCode.RequestTimeout) |
| 36 | + .WaitAndRetryAsync(2, retryAttempt => TimeSpan.FromSeconds(5)); |
| 37 | + |
| 38 | + //503 or 5xx service unavailable - wait 10 secs and retry only once. |
| 39 | + var retryWhenServiceUnavailable = Policy |
| 40 | + .HandleResult<HttpResponseMessage>(r => r.StatusCode == HttpStatusCode.ServiceUnavailable) |
| 41 | + .WaitAndRetryAsync(1, retryAttempt => TimeSpan.FromSeconds(10)); |
| 42 | + |
| 43 | + //401 unauthorized - retry once and do some retry logic + logging |
| 44 | + var retryWhenUnauthorized = Policy |
| 45 | + .HandleResult<HttpResponseMessage>(r => r.StatusCode == HttpStatusCode.Unauthorized) |
| 46 | + .RetryAsync(1, (exception, retryCount) => |
| 47 | + { |
| 48 | + |
| 49 | + this._logger.LogError($"Error occurred retry attempt: {retryCount}, Error details: {exception.Result.ToString()}"); |
| 50 | + //Do some logic here like: |
| 51 | + //RenewAccessToken(); |
| 52 | + }); |
| 53 | + |
| 54 | + IAsyncPolicy<HttpResponseMessage> policyWrap = Policy.WrapAsync(retryWhenThrottling, retryWhenTimeout, retryWhenServiceUnavailable, retryWhenUnauthorized); |
| 55 | + |
| 56 | + services.AddHttpClient("SampleService", client => |
| 57 | + { |
| 58 | + client.BaseAddress = new Uri(@"<You endpoint's base address here>"); |
| 59 | + client.DefaultRequestHeaders.Add("Accept", "application/json"); |
| 60 | + }).AddPolicyHandler(policyWrap); |
| 61 | + |
| 62 | + services.AddControllers(); |
| 63 | + } |
| 64 | + |
| 65 | + // This method gets called by the runtime. Use this method to configure the HTTP request pipeline. |
| 66 | + public void Configure(IApplicationBuilder app, IWebHostEnvironment env, ILogger<Startup> logger) |
| 67 | + { |
| 68 | + this._logger = logger; |
| 69 | + |
| 70 | + if (env.IsDevelopment()) |
| 71 | + { |
| 72 | + app.UseDeveloperExceptionPage(); |
| 73 | + } |
| 74 | + |
| 75 | + app.UseHttpsRedirection(); |
| 76 | + |
| 77 | + app.UseRouting(); |
| 78 | + |
| 79 | + app.UseAuthorization(); |
| 80 | + |
| 81 | + app.UseEndpoints(endpoints => |
| 82 | + { |
| 83 | + endpoints.MapControllers(); |
| 84 | + }); |
| 85 | + } |
| 86 | + } |
87 | 87 | }
|
0 commit comments