From 13b9d32cd2c624dc3867b4e5cdf95595fa3153b9 Mon Sep 17 00:00:00 2001 From: MaxDac Date: Wed, 15 Nov 2023 11:28:38 +0000 Subject: [PATCH] Adding Health Checks `HttpClient` definition (#635) Co-authored-by: Keith Cully <2370032+K-Cully@users.noreply.github.com> --- ...ckHttpClientServiceCollectionExtensions.cs | 32 +++++++++++++++++++ 1 file changed, 32 insertions(+) create mode 100644 src/Diagnostics.HealthChecks/HealthCheckHttpClientServiceCollectionExtensions.cs diff --git a/src/Diagnostics.HealthChecks/HealthCheckHttpClientServiceCollectionExtensions.cs b/src/Diagnostics.HealthChecks/HealthCheckHttpClientServiceCollectionExtensions.cs new file mode 100644 index 00000000..a6c8195f --- /dev/null +++ b/src/Diagnostics.HealthChecks/HealthCheckHttpClientServiceCollectionExtensions.cs @@ -0,0 +1,32 @@ +// Copyright (c) Microsoft Corporation. All rights reserved. +// Licensed under the MIT license. + +using System.Net.Http; +using Microsoft.Extensions.DependencyInjection; + +namespace Microsoft.Omex.Extensions.Diagnostics.HealthChecks; + +/// +/// A collection of extension methods to define for Health Checks +/// in the DI. +/// +public static class HealthCheckHttpClientServiceCollectionExtensions +{ + /// + /// Defines a in the DI designed for Health Checks. + /// The main difference is that these clients in development will ignore SSL errors. + /// This is an acceptable compromise considering that the health checks are executed in the same machine + /// as the Service Fabric service itself, and that each client will be specifically used for the + /// health checks, as the name parameter in input is mandatory. + /// Every other uses of this extension methods apart from health checks should be avoided. + /// + /// The services. + /// The logical name of the to configure. + /// The . + public static IHttpClientBuilder AddHealthCheckHttpClient(this IServiceCollection services, string name) => + services.AddHttpClient(name) + .ConfigurePrimaryHttpMessageHandler(() => new HttpClientHandler + { + ServerCertificateCustomValidationCallback = (_, _, _, _) => true + }); +}