Skip to content

Commit cd1f11b

Browse files
committed
Create new polly policies for each service instead of use the same, in order to resolve problems with the execution context name
1 parent fb15ed1 commit cd1f11b

3 files changed

Lines changed: 70 additions & 54 deletions

File tree

src/ApiGateways/Mobile.Bff.Shopping/aggregator/Startup.cs

Lines changed: 22 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
using System;
1616
using System.Collections.Generic;
1717
using System.IdentityModel.Tokens.Jwt;
18+
using System.Net.Http;
1819

1920
namespace Microsoft.eShopOnContainers.Mobile.Shopping.HttpAggregator
2021
{
@@ -147,30 +148,37 @@ public static IServiceCollection AddHttpServices(this IServiceCollection service
147148
services.AddSingleton<IHttpContextAccessor, HttpContextAccessor>();
148149

149150
//register http services
150-
var retriesWithExponentialBackoff = HttpPolicyExtensions
151-
.HandleTransientHttpError()
152-
.WaitAndRetryAsync(6, retryAttempt => TimeSpan.FromSeconds(Math.Pow(2, retryAttempt)));
153-
154-
var circuitBreaker = HttpPolicyExtensions
155-
.HandleTransientHttpError()
156-
.CircuitBreakerAsync(5, TimeSpan.FromSeconds(30));
157-
158151
services.AddHttpClient<IBasketService, BasketService>()
159152
.AddHttpMessageHandler<HttpClientAuthorizationDelegatingHandler>()
160-
.AddPolicyHandler(retriesWithExponentialBackoff)
161-
.AddPolicyHandler(circuitBreaker);
153+
.AddPolicyHandler(GetRetryPolicy())
154+
.AddPolicyHandler(GetCircuitBreakerPolicy());
162155

163156
services.AddHttpClient<ICatalogService, CatalogService>()
164-
.AddPolicyHandler(retriesWithExponentialBackoff)
165-
.AddPolicyHandler(circuitBreaker);
157+
.AddPolicyHandler(GetRetryPolicy())
158+
.AddPolicyHandler(GetCircuitBreakerPolicy());
166159

167160
services.AddHttpClient<IOrderApiClient, OrderApiClient>()
168-
.AddPolicyHandler(retriesWithExponentialBackoff)
169-
.AddPolicyHandler(circuitBreaker);
161+
.AddPolicyHandler(GetRetryPolicy())
162+
.AddPolicyHandler(GetCircuitBreakerPolicy());
170163

171164

172165

173166
return services;
174167
}
168+
169+
static IAsyncPolicy<HttpResponseMessage> GetRetryPolicy()
170+
{
171+
return HttpPolicyExtensions
172+
.HandleTransientHttpError()
173+
.OrResult(msg => msg.StatusCode == System.Net.HttpStatusCode.NotFound)
174+
.WaitAndRetryAsync(6, retryAttempt => TimeSpan.FromSeconds(Math.Pow(2, retryAttempt)));
175+
176+
}
177+
static IAsyncPolicy<HttpResponseMessage> GetCircuitBreakerPolicy()
178+
{
179+
return HttpPolicyExtensions
180+
.HandleTransientHttpError()
181+
.CircuitBreakerAsync(5, TimeSpan.FromSeconds(30));
182+
}
175183
}
176184
}

src/ApiGateways/Web.Bff.Shopping/aggregator/Startup.cs

Lines changed: 22 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
using System;
1717
using System.Collections.Generic;
1818
using System.IdentityModel.Tokens.Jwt;
19+
using System.Net.Http;
1920

2021
namespace Microsoft.eShopOnContainers.Web.Shopping.HttpAggregator
2122
{
@@ -150,32 +151,37 @@ public static IServiceCollection AddApplicationServices(this IServiceCollection
150151

151152
//register http services
152153

153-
var retriesWithExponentialBackoff = HttpPolicyExtensions
154-
.HandleTransientHttpError()
155-
.Or<TimeoutRejectedException>()
156-
.OrResult(message => message.StatusCode == System.Net.HttpStatusCode.NotFound)
157-
.WaitAndRetryAsync(6, retryAttempt => TimeSpan.FromSeconds(Math.Pow(2, retryAttempt)));
158-
159-
var circuitBreaker = HttpPolicyExtensions
160-
.HandleTransientHttpError()
161-
.CircuitBreakerAsync(5, TimeSpan.FromSeconds(30));
162-
163154
services.AddHttpClient<IBasketService, BasketService>()
164155
.AddHttpMessageHandler<HttpClientAuthorizationDelegatingHandler>()
165-
.AddPolicyHandler(retriesWithExponentialBackoff)
166-
.AddPolicyHandler(circuitBreaker);
156+
.AddPolicyHandler(GetRetryPolicy())
157+
.AddPolicyHandler(GetCircuitBreakerPolicy());
167158

168159
services.AddHttpClient<ICatalogService, CatalogService>()
169-
.AddPolicyHandler(retriesWithExponentialBackoff)
170-
.AddPolicyHandler(circuitBreaker);
160+
.AddPolicyHandler(GetRetryPolicy())
161+
.AddPolicyHandler(GetCircuitBreakerPolicy());
171162

172163
services.AddHttpClient<IOrderApiClient, OrderApiClient>()
173164
.AddHttpMessageHandler<HttpClientAuthorizationDelegatingHandler>()
174-
.AddPolicyHandler(retriesWithExponentialBackoff)
175-
.AddPolicyHandler(circuitBreaker);
165+
.AddPolicyHandler(GetRetryPolicy())
166+
.AddPolicyHandler(GetCircuitBreakerPolicy());
176167

177168

178169
return services;
179170
}
171+
172+
static IAsyncPolicy<HttpResponseMessage> GetRetryPolicy()
173+
{
174+
return HttpPolicyExtensions
175+
.HandleTransientHttpError()
176+
.OrResult(msg => msg.StatusCode == System.Net.HttpStatusCode.NotFound)
177+
.WaitAndRetryAsync(6, retryAttempt => TimeSpan.FromSeconds(Math.Pow(2, retryAttempt)));
178+
179+
}
180+
static IAsyncPolicy<HttpResponseMessage> GetCircuitBreakerPolicy()
181+
{
182+
return HttpPolicyExtensions
183+
.HandleTransientHttpError()
184+
.CircuitBreakerAsync(5, TimeSpan.FromSeconds(30));
185+
}
180186
}
181187
}

src/Web/WebMVC/Startup.cs

Lines changed: 26 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
using StackExchange.Redis;
1818
using System;
1919
using System.IdentityModel.Tokens.Jwt;
20+
using System.Net.Http;
2021
using WebMVC.Infrastructure;
2122
using WebMVC.Infrastructure.Middlewares;
2223
using WebMVC.Services;
@@ -168,18 +169,6 @@ public static IServiceCollection AddHttpClientServices(this IServiceCollection s
168169
{
169170
services.AddSingleton<IHttpContextAccessor, HttpContextAccessor>();
170171

171-
//Using fluent client configuration of Polly policies
172-
//(CDLTLL) Instead of hardcoded values, use: configuration["HttpClientMaxNumberRetries"], configuration["SecondsBaseForExponentialBackoff"]
173-
174-
var retriesWithExponentialBackoff = HttpPolicyExtensions
175-
.HandleTransientHttpError()
176-
.OrResult(msg=>msg.StatusCode == System.Net.HttpStatusCode.NotFound)
177-
.WaitAndRetryAsync(6,retryAttempt => TimeSpan.FromSeconds(Math.Pow(2, retryAttempt)));
178-
179-
var circuitBreaker = HttpPolicyExtensions
180-
.HandleTransientHttpError()
181-
.CircuitBreakerAsync(5, TimeSpan.FromSeconds(30));
182-
183172
//register delegating handlers
184173
services.AddTransient<HttpClientAuthorizationDelegatingHandler>();
185174
services.AddTransient<HttpClientRequestIdDelegatingHandler>();
@@ -190,32 +179,32 @@ public static IServiceCollection AddHttpClientServices(this IServiceCollection s
190179
services.AddHttpClient<IBasketService, BasketService>()
191180
.SetHandlerLifetime(TimeSpan.FromMinutes(5)) //Sample. Default lifetime is 2 minutes
192181
.AddHttpMessageHandler<HttpClientAuthorizationDelegatingHandler>()
193-
.AddPolicyHandler(retriesWithExponentialBackoff)
194-
.AddPolicyHandler(circuitBreaker);
182+
.AddPolicyHandler(GetRetryPolicy())
183+
.AddPolicyHandler(GetCircuitBreakerPolicy());
195184

196185
services.AddHttpClient<ICatalogService, CatalogService>()
197-
.AddPolicyHandler(retriesWithExponentialBackoff)
198-
.AddPolicyHandler(circuitBreaker);
186+
.AddPolicyHandler(GetRetryPolicy())
187+
.AddPolicyHandler(GetCircuitBreakerPolicy());
199188

200189
services.AddHttpClient<IOrderingService, OrderingService>()
201190
.AddHttpMessageHandler<HttpClientAuthorizationDelegatingHandler>()
202191
.AddHttpMessageHandler<HttpClientRequestIdDelegatingHandler>()
203-
.AddPolicyHandler(retriesWithExponentialBackoff)
204-
.AddPolicyHandler(circuitBreaker);
192+
.AddPolicyHandler(GetRetryPolicy())
193+
.AddPolicyHandler(GetCircuitBreakerPolicy());
205194

206195
services.AddHttpClient<ICampaignService, CampaignService>()
207196
.AddHttpMessageHandler<HttpClientAuthorizationDelegatingHandler>()
208-
.AddPolicyHandler(retriesWithExponentialBackoff)
209-
.AddPolicyHandler(circuitBreaker);
197+
.AddPolicyHandler(GetRetryPolicy())
198+
.AddPolicyHandler(GetCircuitBreakerPolicy());
210199

211200
services.AddHttpClient<ILocationService, LocationService>()
212201
.AddHttpMessageHandler<HttpClientAuthorizationDelegatingHandler>()
213-
.AddPolicyHandler(retriesWithExponentialBackoff)
214-
.AddPolicyHandler(circuitBreaker);
202+
.AddPolicyHandler(GetRetryPolicy())
203+
.AddPolicyHandler(GetCircuitBreakerPolicy());
215204

216205
//add custom application services
217206
services.AddTransient<IIdentityParser<ApplicationUser>, IdentityParser>();
218-
207+
219208
return services;
220209
}
221210

@@ -273,6 +262,19 @@ public static IServiceCollection AddCustomAuthentication(this IServiceCollection
273262
return services;
274263
}
275264

276-
}
265+
static IAsyncPolicy<HttpResponseMessage> GetRetryPolicy()
266+
{
267+
return HttpPolicyExtensions
268+
.HandleTransientHttpError()
269+
.OrResult(msg => msg.StatusCode == System.Net.HttpStatusCode.NotFound)
270+
.WaitAndRetryAsync(6, retryAttempt => TimeSpan.FromSeconds(Math.Pow(2, retryAttempt)));
277271

272+
}
273+
static IAsyncPolicy<HttpResponseMessage> GetCircuitBreakerPolicy()
274+
{
275+
return HttpPolicyExtensions
276+
.HandleTransientHttpError()
277+
.CircuitBreakerAsync(5, TimeSpan.FromSeconds(30));
278+
}
279+
}
278280
}

0 commit comments

Comments
 (0)