From 8844aaba75e2717fdd38788e572c987370cd768c Mon Sep 17 00:00:00 2001 From: Cesar De la Torre Llorente Date: Mon, 21 May 2018 17:04:18 -0700 Subject: [PATCH 1/3] Adding an example of a HttpMessageHandler's Lifetime to 5 minutes in the pool. The default lifetime of HttpMessageHandler instances in the pool is 2 minutes. --- src/Web/WebMVC/Startup.cs | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/src/Web/WebMVC/Startup.cs b/src/Web/WebMVC/Startup.cs index 9cc5921a2..655ab1e68 100644 --- a/src/Web/WebMVC/Startup.cs +++ b/src/Web/WebMVC/Startup.cs @@ -182,9 +182,12 @@ namespace Microsoft.eShopOnContainers.WebMVC services.AddTransient(); services.AddTransient(); - //add http client servicse + services.AddHttpClient("extendedhandlerlifetime").SetHandlerLifetime(TimeSpan.FromMinutes(5)); + + //add http client services services.AddHttpClient() .AddHttpMessageHandler() + .SetHandlerLifetime(TimeSpan.FromMinutes(5)) //Sample. Default lifetime is 2 minutes .AddPolicyHandler(retriesWithExponentialBackoff) .AddPolicyHandler(circuitBreaker); From 1fa6242c1c1a2ad46b100a587fef6108ae468182 Mon Sep 17 00:00:00 2001 From: Cesar De la Torre Llorente Date: Mon, 21 May 2018 18:11:27 -0700 Subject: [PATCH 2/3] Added opt-in HttpClientLogging config --- src/Web/WebMVC/Startup.cs | 26 ++++++++++++++++++++++---- 1 file changed, 22 insertions(+), 4 deletions(-) diff --git a/src/Web/WebMVC/Startup.cs b/src/Web/WebMVC/Startup.cs index 655ab1e68..3df96da2c 100644 --- a/src/Web/WebMVC/Startup.cs +++ b/src/Web/WebMVC/Startup.cs @@ -36,10 +36,11 @@ namespace Microsoft.eShopOnContainers.WebMVC public void ConfigureServices(IServiceCollection services) { services.AddAppInsight(Configuration) - .AddHealthChecks(Configuration) - .AddCustomMvc(Configuration) - .AddHttpClientServices(Configuration) - .AddCustomAuthentication(Configuration); + .AddHealthChecks(Configuration) + .AddCustomMvc(Configuration) + .AddHttpClientServices(Configuration) + //.AddHttpClientLogging(Configuration) //Opt-in HttpClientLogging config + .AddCustomAuthentication(Configuration); } // This method gets called by the runtime. Use this method to configure the HTTP request pipeline. @@ -216,6 +217,23 @@ namespace Microsoft.eShopOnContainers.WebMVC return services; } + + public static IServiceCollection AddHttpClientLogging(this IServiceCollection services, IConfiguration configuration) + { + services.AddLogging(b => + { + b.AddFilter((category, level) => true); // Spam the world with logs. + + // Add console logger so we can see all the logging produced by the client by default. + b.AddConsole(c => c.IncludeScopes = true); + + // Add console logger + b.AddDebug(); + }); + + return services; + } + public static IServiceCollection AddCustomAuthentication(this IServiceCollection services, IConfiguration configuration) { var useLoadTest = configuration.GetValue("UseLoadTest"); From 6127457e8e92f2a579bd1e4a9acda228fc006909 Mon Sep 17 00:00:00 2001 From: Cesar De la Torre Llorente Date: Mon, 21 May 2018 20:06:39 -0700 Subject: [PATCH 3/3] Changed order in pipeline so SetHandlerLifetime() is first, right on the on the IHttpClientBuilder that is returned when creating the Typed Client. --- src/Web/WebMVC/Startup.cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Web/WebMVC/Startup.cs b/src/Web/WebMVC/Startup.cs index 3df96da2c..34c79d010 100644 --- a/src/Web/WebMVC/Startup.cs +++ b/src/Web/WebMVC/Startup.cs @@ -187,8 +187,8 @@ namespace Microsoft.eShopOnContainers.WebMVC //add http client services services.AddHttpClient() - .AddHttpMessageHandler() .SetHandlerLifetime(TimeSpan.FromMinutes(5)) //Sample. Default lifetime is 2 minutes + .AddHttpMessageHandler() .AddPolicyHandler(retriesWithExponentialBackoff) .AddPolicyHandler(circuitBreaker);