Soenneker.Utils.HttpClientCache 4.0.2041

Prefix Reserved
There is a newer version of this package available.
See the version list below for details.
dotnet add package Soenneker.Utils.HttpClientCache --version 4.0.2041
                    
NuGet\Install-Package Soenneker.Utils.HttpClientCache -Version 4.0.2041
                    
This command is intended to be used within the Package Manager Console in Visual Studio, as it uses the NuGet module's version of Install-Package.
<PackageReference Include="Soenneker.Utils.HttpClientCache" Version="4.0.2041" />
                    
For projects that support PackageReference, copy this XML node into the project file to reference the package.
<PackageVersion Include="Soenneker.Utils.HttpClientCache" Version="4.0.2041" />
                    
Directory.Packages.props
<PackageReference Include="Soenneker.Utils.HttpClientCache" />
                    
Project file
For projects that support Central Package Management (CPM), copy this XML node into the solution Directory.Packages.props file to version the package.
paket add Soenneker.Utils.HttpClientCache --version 4.0.2041
                    
#r "nuget: Soenneker.Utils.HttpClientCache, 4.0.2041"
                    
#r directive can be used in F# Interactive and Polyglot Notebooks. Copy this into the interactive tool or source code of the script to reference the package.
#:package Soenneker.Utils.HttpClientCache@4.0.2041
                    
#:package directive can be used in C# file-based apps starting in .NET 10 preview 4. Copy this into a .cs file before any lines of code to reference the package.
#addin nuget:?package=Soenneker.Utils.HttpClientCache&version=4.0.2041
                    
Install as a Cake Addin
#tool nuget:?package=Soenneker.Utils.HttpClientCache&version=4.0.2041
                    
Install as a Cake Tool

alternate text is missing from this package README image alternate text is missing from this package README image alternate text is missing from this package README image alternate text is missing from this package README image

Soenneker.Utils.HttpClientCache

Provides lazily created, reusable HttpClient instances keyed by application-defined identifiers.

Installation

dotnet add package Soenneker.Utils.HttpClientCache

Registration

using Soenneker.Utils.HttpClientCache.Registrar;

services.AddHttpClientCacheAsSingleton();

Use AddHttpClientCacheAsScoped() only when every dependency-injection scope should own and dispose an independent cache.

Create or retrieve a client

using System.Net;
using Soenneker.Dtos.HttpClientOptions;
using Soenneker.Utils.HttpClientCache.Abstract;

HttpClient client = await cache.Get(
    "catalog-api",
    static () => new HttpClientOptions
    {
        BaseAddress = new Uri("https://catalog.example.com/"),
        Timeout = TimeSpan.FromSeconds(30),
        ConnectTimeout = TimeSpan.FromSeconds(5),
        AutomaticDecompression = DecompressionMethods.GZip | DecompressionMethods.Deflate,
        DefaultRequestHeaders = new Dictionary<string, string>
        {
            ["Accept"] = "application/json"
        }
    },
    cancellationToken);

The options factory runs only when that key is first initialized. Later calls with the same key return the same HttpClient and ignore new factories/options. Use a stable, unique key for each logical client configuration.

Do not dispose a returned client. Remove and dispose one cache entry with Remove/RemoveSync, or dispose the cache to release all owned clients and dedicated handlers.

Handler behavior

Handlers with the same transport options can be shared across cache keys. Cookies are disabled on shared handlers. Setting UseCookieContainer = true creates a dedicated, cache-owned handler and cookie container for that client so cookies are not shared between keys.

ModifyPrimaryHandler, Proxy, SslOptions, or delegating handlers also require dedicated transports. Delegating handler factories run once during client creation; each must return a new handler with InnerHandler unset. The first factory in the list becomes the outermost handler.

ModifyClient runs once after base address, timeout, and default headers are applied. It can perform asynchronous initialization.

Security and lifecycle notes

  • Default request headers are added without semantic validation. Use only trusted header names and values.
  • Automatic redirects can forward non-Authorization custom headers to another host. Disable redirects for clients carrying secrets in custom headers unless every redirect target is trusted.
  • Custom certificate validation in SslOptions or ModifyPrimaryHandler changes the TLS trust boundary; avoid permissive callbacks.
  • The cache does not retry, enforce successful status codes, or dispose response messages. Callers own each response and response stream.
  • Browser targets do not support custom primary/delegating handler configuration.

Synchronous GetSync overloads are available for callers that cannot use asynchronous initialization. Prefer asynchronous Get when an options factory performs I/O.

Product Compatible and additional computed target framework versions.
.NET net10.0 is compatible.  net10.0-android was computed.  net10.0-browser was computed.  net10.0-ios was computed.  net10.0-maccatalyst was computed.  net10.0-macos was computed.  net10.0-tvos was computed.  net10.0-windows was computed. 
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.

NuGet packages (145)

Showing the top 5 NuGet packages that depend on Soenneker.Utils.HttpClientCache:

Package Downloads
Soenneker.Cosmos.Client

A utility library for Azure Cosmos client accessibility

Soenneker.Blob.Container

A utility library for Azure Blob storage container operations

Soenneker.Validators.Email.Disposable.Online

A validation module checking for disposable email addresses via online sources

Soenneker.Queue.Client

A utility library for Azure Queue (Storage) client accessibility

Soenneker.GitHub.Client.Http

Provides a configured, cached HttpClient for GitHub's REST API.

GitHub repositories

This package is not used by any popular GitHub repositories.

Version Downloads Last Updated
4.0.2051 5,938 9/1/2026
4.0.2050 6,223 8/31/2026
4.0.2049 3,151 8/31/2026
4.0.2048 5,579 8/31/2026
4.0.2047 415 8/31/2026
4.0.2046 522 8/31/2026
4.0.2045 1,147 8/31/2026
4.0.2044 3,426 8/30/2026
4.0.2043 3,381 8/30/2026
4.0.2041 1,193 8/30/2026
4.0.2040 2,074 8/30/2026
4.0.2039 77 8/30/2026
4.0.2038 2,343 8/30/2026
4.0.2037 312 8/30/2026
4.0.2035 1,170 8/29/2026
4.0.2033 4,320 8/29/2026
4.0.2032 565 8/29/2026
4.0.2031 39,180 8/26/2026
4.0.2030 5,068 8/26/2026
4.0.2029 1,887 8/26/2026
Loading failed

Update dependency Soenneker.Dtos.HttpClientOptions to 4.0.30 (#2397)