HttpClientFactory vs new HttpClient
Why is `new HttpClient()` per request bad in .NET, and how does IHttpClientFactory help?
Answers use simple, clear English.
Quick interview answer
Creating/disposing HttpClient per call can exhaust sockets (TIME_WAIT) and ignore DNS changes if a static long-lived client is misused. IHttpClientFactory manages handlers, DNS refresh, named/typed clients, and Polly policies (retry/circuit breaker) centrally.
Detailed answer
Creating/disposing HttpClient per call can exhaust sockets (TIME_WAIT) and ignore DNS changes if a static long-lived client is misused. IHttpClientFactory manages handlers, DNS refresh, named/typed clients, and Polly policies (retry/circuit breaker) centrally.
Real example & use case
AddHttpClient<PaymentClient>(...).AddPolicyHandler(retryPolicy).
Pros & cons
Pros: stable sockets + policies. Cons: retries need idempotent APIs.