httpclient-factory

IHttpClientFactory and typed HTTP clients for .NET 10 applications. Covers named/typed/keyed clients, DelegatingHandlers, resilience with Microsoft.Extensions.Http.Resilience, and testing patterns. Load this skill when configuring HTTP clients, adding retry/circuit breaker policies, or when the user

By codewithmukesh · 1,190 installs

npx skills add codewithmukesh/dotnet-claude-kit --skill httpclient-factory

Source repository · Upstream listing

HttpClient Factory Core Principles 1. Never new HttpClient() per request — Raw HttpClient creation causes socket exhaustion under load and ignores DNS changes. Use IHttpClientFactory to manage handler lifetimes. 2. Keyed clients over typed clients — Keyed DI ( .AddAsKeyed() ) is the recommended pattern in .NET 10. Typed clients captured in singletons silently break handler rotation. 3. Resilience is not optional — Every external HTTP call needs retry, circuit breaker, and timeout. AddStandardResilienceHandler() provides sensible defaults in one line. 4. DelegatingHandlers for cross cutting concerns — Auth tokens, correlation IDs, and logging belong in the handler pipeline, not scattered across service methods. Patterns Named Client with Resilience Keyed Client (Recommended in .NET 10) Combines named client configurability with direct injection. No string lookups. Global opt in: builder.Services.ConfigureHttpClientDefaults(b = b.AddAsKeyed()); Standard Resilience Handler AddStandardResilienceHandler() chains 5 strategies: Strategy Default Rate limiter 1000 concurrent requests Total timeout 30 seconds Retry 3 retries, exponential backoff with jitter Circuit breaker Opens at 10% failure rate Attempt timeout 10 seconds per attempt DelegatingHandler for Auth Token Injection DelegatingHandler for Correlation ID Propagation SocketsHttpHandler Configuration Testing with Mock Handler Anti patterns Don't Create HttpClient Per Request Don't Capture Typed Clients in Singletons Don't Mutate DefaultRequestHeaders on Shared Clients Don't Forget CancellationToken Don't Stack Multiple Resilience Handlers Decision Guide Scenario Recommendation New .NET 10 project Keyed clients with AddAsKeyed() Singleton service needs HttpClient Named client via IHttpClientFactory or keyed singleton External API calls AddStandardResilienceHandler() on every client Auth token injection DelegatingHandler registered with AddHttpMessageHandler Hedging (parallel requests) AddStandardHedgingHandler() for latency sensitive calls Non idempotent methods DisableForUnsafeHttpMethods() on retry options Custom retry logic AddResilienceHandler("name", builder = ...) Connection pooling control UseSocketsHttpHandler with PooledConnectionLifetime API client generation Refit with AddRefitClient<T () Integration testing Custom HttpMessageHandler or MockHttpMessageHandler