2

I have a third party api I am using for a number of my Services in .NET Core Web API project.
So, I have something like below:

services.AddHttpClient<IUserService, UserService>(client =>
{
    client.BaseAddress = new Uri(Environment.GetEnvironmentVariable("EXTERNAL_SERVICE_BASE_URL") ??
                                 throw new ArgumentNullException("EXTERNAL_SERVICE_BASE_URLcannot be null"));

    client.DefaultRequestHeaders.Add("removed", "removed");

}).ConfigurePrimaryHttpMessageHandler(() =>
{
    HttpClientHandler clientHandler = new HttpClientHandler
    {
        ClientCertificateOptions = ClientCertificateOption.Manual,
        ServerCertificateCustomValidationCallback = (sender, cert, chain, sslPolicyErrors) => true
    };
    clientHandler.ClientCertificates.Add(new X509Certificate2(securitySession.CertificateData.Data, securitySession.CertificateData.Password));
    return new HttpClientXRayTracingHandler(clientHandler);
})
.AddPolicyHandler(HttpPolicies.GetRetryPolicy())
.SetHandlerLifetime(TimeSpan.FromSeconds(1));

This is fine and I can access the HttpClient in UserService and call my External API fine. However what I am going to need now is more Services in my code - but they use the same EXTERNAL_API - so lets say and AccountService and a CustomerService - and i'll end up with this:

services.AddHttpClient<IAccountService, AccountService>(client =>
{
    client.BaseAddress = new Uri(Environment.GetEnvironmentVariable("EXTERNAL_SERVICE_BASE_URL") ??
                                 throw new ArgumentNullException("EXTERNAL_SERVICE_BASE_URLcannot be null"));
    client.DefaultRequestHeaders.Add("removed", "removed");
}).ConfigurePrimaryHttpMessageHandler(() =>
{
    HttpClientHandler clientHandler = new HttpClientHandler
    {
        ClientCertificateOptions = ClientCertificateOption.Manual,
        ServerCertificateCustomValidationCallback = (sender, cert, chain, sslPolicyErrors) => true
    };
    clientHandler.ClientCertificates.Add(new X509Certificate2(securitySession.CertificateData.Data, securitySession.CertificateData.Password));
    return new HttpClientXRayTracingHandler(clientHandler);
})
.AddPolicyHandler(HttpPolicies.GetRetryPolicy())
.SetHandlerLifetime(TimeSpan.FromSeconds(1));
services.AddHttpClient<ICustomerService, CustomerService>(client =>
{
    client.BaseAddress = new Uri(Environment.GetEnvironmentVariable("EXTERNAL_SERVICE_BASE_URL") ??
                                 throw new ArgumentNullException("EXTERNAL_SERVICE_BASE_URLcannot be null"));
    client.DefaultRequestHeaders.Add("removed", "removed");
}).ConfigurePrimaryHttpMessageHandler(() =>
{
    HttpClientHandler clientHandler = new HttpClientHandler
    {
        ClientCertificateOptions = ClientCertificateOption.Manual,
        ServerCertificateCustomValidationCallback = (sender, cert, chain, sslPolicyErrors) => true
    };
    clientHandler.ClientCertificates.Add(new X509Certificate2(securitySession.CertificateData.Data, securitySession.CertificateData.Password));
    return new HttpClientXRayTracingHandler(clientHandler);
})
.AddPolicyHandler(HttpPolicies.GetRetryPolicy())
.SetHandlerLifetime(TimeSpan.FromSeconds(1));

The only change being the Service in my App that is using the HttpClient. Is there a way I can move all the common plumbing of the wire up to a private method that can be called when I am adding the http client to each of my services?

Peter Csala
  • 17,736
  • 16
  • 35
  • 75
Ctrl_Alt_Defeat
  • 3,933
  • 12
  • 66
  • 116

2 Answers2

1

You can create an IServiceCollection extension method:

public static class ServiceCollectionExtensions
{
    // Change securitySession parameter to the appropriate type
    public static void AddCustomHttpClient<TClient, TImplementation>(this IServiceCollection services, SecuritySession securitySession)
        where TClient : class
        where TImplementation : class, TClient
    {
        services.AddHttpClient<TClient, TImplementation>(client =>
        {
            client.BaseAddress = new Uri(Environment.GetEnvironmentVariable("EXTERNAL_SERVICE_BASE_URL") ??
                                         throw new ArgumentNullException("EXTERNAL_SERVICE_BASE_URLcannot be null"));

            client.DefaultRequestHeaders.Add("removed", "removed");

        }).ConfigurePrimaryHttpMessageHandler(() =>
        {
            HttpClientHandler clientHandler = new HttpClientHandler
            {
                ClientCertificateOptions = ClientCertificateOption.Manual,
                ServerCertificateCustomValidationCallback = (sender, cert, chain, sslPolicyErrors) => true
            };
            clientHandler.ClientCertificates.Add(new X509Certificate2(securitySession.CertificateData.Data, securitySession.CertificateData.Password));
            return new HttpClientXRayTracingHandler(clientHandler);
        })
        .AddPolicyHandler(HttpPolicies.GetRetryPolicy())
        .SetHandlerLifetime(TimeSpan.FromSeconds(1));
    }
}

And use it like this:

services.AddCustomHttpClient<IUserService, UserService>(securitySession);
services.AddCustomHttpClient<IAccountService, AccountService>(securitySession);
services.AddCustomHttpClient<ICustomerService, CustomerService>(securitySession);
Dimitris Maragkos
  • 8,932
  • 2
  • 8
  • 26
  • this looks like exactly what I need -would the same httpclient then be used if an API was called in the UserService folllowed by an API call in AccountService? – Ctrl_Alt_Defeat Oct 08 '22 at 22:30
  • 1
    No, I think this will register a different HttpClient for each service. But all of them will have the same configuration. – Dimitris Maragkos Oct 09 '22 at 11:16
1

I would suggest to have a single named client

services.AddHttpClient("commonClient", client =>
{
    client.BaseAddress = new Uri(Environment.GetEnvironmentVariable("EXTERNAL_SERVICE_BASE_URL") ??
                                 throw new ArgumentNullException("EXTERNAL_SERVICE_BASE_URL cannot be null"));

    client.DefaultRequestHeaders.Add("removed", "removed");

}).ConfigurePrimaryHttpMessageHandler(() =>
{
    HttpClientHandler clientHandler = new HttpClientHandler
    {
        ClientCertificateOptions = ClientCertificateOption.Manual,
        ServerCertificateCustomValidationCallback = (sender, cert, chain, sslPolicyErrors) => true
    };
    clientHandler.ClientCertificates.Add(new X509Certificate2(securitySession.CertificateData.Data, securitySession.CertificateData.Password));
    return new HttpClientXRayTracingHandler(clientHandler);
})
.AddPolicyHandler(HttpPolicies.GetRetryPolicy())
.SetHandlerLifetime(TimeSpan.FromSeconds(1));

and 3 typed clients

services.AddHttpClient<IUserService, UserService>();
services.AddHttpClient<IAccountService, AccountService>();
services.AddHttpClient<ICustomerService, CustomerService>();

The usage of these components are a bit different than using just a named or just a typed client

readonly IUserService client;
public XYZController(IHttpClientFactory namedClientFactory, ITypedHttpClientFactory<UserService> typedClientFactory)
{
    var namedClient = namedClientFactory.CreateClient("commonClient");
    client = typedClientFactory.CreateClient(namedClient);
}
  • First via the IHttpClientFactory we retrieve the "commonClient"
  • Then we create a new instance of UserService by passing the previously retrieved named client

NOTE: The type parameter of ITypedHttpClientFactory must be the concrete type not the interface otherwise you would receive an InvalidOperationException

Peter Csala
  • 17,736
  • 16
  • 35
  • 75
  • dont You mean services.AddSingelton/Scoped(); ? and inside get this 'named client' ? if no then from where this UserService will get configuration ? – d00lar Oct 11 '22 at 10:07
  • @d00lar If I would register them as Singleton or Scoped then I would not able to create them through the `ITypedHttpClientFactory` interface. [Here](https://stackoverflow.com/questions/73707380/cannot-get-polly-retry-http-calls-when-given-exceptions-are-raised/73715343#73715343) I have detailed how the different HttpClient types can be used. – Peter Csala Oct 11 '22 at 10:15
  • @d00lar And [here](https://stackoverflow.com/questions/73464868/what-is-the-lifetime-of-a-typed-httpclient-instance-from-ihttpclientfactory-wher/73838672#73838672) I have detailed how does `AddHttpClient` extension method work. – Peter Csala Oct 11 '22 at 10:16
  • 1
    ok thanks it is for controller -now isee. ok i tried something like this in blazor wasm - there it need to be like scoped and get this named client from ctor'ed IHttpClientFactory - otherwise it does not allow injecting lets say IUserService into component - it yeals that no siutable ctor was found when added via addhttpclient – d00lar Oct 11 '22 at 10:35