Table of Contents

Class HttpClientFactory

Namespace
Homa.Sdk.Foundation
Assembly
Homa.Sdk.Foundation.dll

Factory producing HttpClient instances. Holds a cache of http client instances per Uri, that are valid for 15 minutes.

public class HttpClientFactory
Inheritance
HttpClientFactory
Inherited Members

Remarks

This is a substitute for IHttpClientFactory available in 2.0 NET Standard and .NET 6 https://learn.microsoft.com/en-us/aspnet/core/fundamentals/http-requests?view=aspnetcore-8.0 Absolutely never cache instances received through this factory. The entire idea of this factory is to provide resilient HttpClient instances against DNS timeouts, by rotating client instances w/o managing the underlying HttpClientMessageHandler instance. Instead of rotating HttpClientHandler this factory rotates HttpClients In the future, with DI, a lot of internal code can be moved to Options patterns or other ways of configuring the behavior

Constructors

HttpClientFactory(ILogger)

As Homa SDK does not provide a DI mechanism yet use Instance to take full benefit of the factory.

public HttpClientFactory(ILogger logger)

Parameters

logger ILogger

Remarks

The requirement of the constructor being public at this moment only comes from tests, but once we have DI in place, we would expect this constructor to be public anyway, so it's better to have it public from the start.

Fields

Instance

Shared factory instance for public use

public static readonly HttpClientFactory Instance

Field Value

HttpClientFactory

Methods

Configure(string, HttpClientOptions)

Allows to provide configuration options for named HttpClient instances.

public void Configure(string name, HttpClientOptions options)

Parameters

name string

Name of the client.

options HttpClientOptions

Options. See HttpClientOptions for supported options.

Exceptions

ArgumentNullException

name is null.

CreateClient(string)

Creates and configures an HttpClient instance.

public HttpClient CreateClient(string name)

Parameters

name string

The logical name of the client to create.

Returns

HttpClient

Remarks

Do not dispose HttpClient instance received via this method.

SetCustomHandler(string, HttpMessageHandler)

Allows to set a custom HttpMessageHandler for a client under a given name.

[Obsolete("Use Configure(string, HttpClientOptions) instead.")]
public void SetCustomHandler(string name, HttpMessageHandler handler)

Parameters

name string

Name of the client

handler HttpMessageHandler

Handler to use

Exceptions

ArgumentNullException

Thrown when name is null.