.NET Developer Interview Question

Why should you use IHttpClientFactory rather than creating a new HttpClient for each request?

What the interviewer is probing, how to structure your answer, and a spoken example you can adapt.

Quick answer

Creating and disposing an HttpClient per request can exhaust sockets, because closed connections linger in TIME_WAIT. Keeping one static HttpClient forever avoids that but can miss DNS changes, since pooled connections are reused indefinitely. IHttpClientFactory solves both by pooling and recycling the underlying handlers, and it gives you one place to configure named or typed clients, timeouts, headers and resilience policies such as retries.

Why interviewers ask this

This is a classic .NET question because the obvious code, a using block around HttpClient, is wrong in a way that only shows up under load. The interviewer wants both failure modes, socket exhaustion and stale DNS, and an explanation of how the factory addresses them. Mentioning typed clients, resilience policies, and a static client with a pooled connection lifetime as a valid alternative shows current, practical knowledge.

How to structure your answer

  • Explain socket exhaustion from short lived clients.
  • Explain stale DNS from a single client that lives forever.
  • Describe how the factory pools and recycles handlers.
  • Cover typed clients, configuration and resilience.

Example answer

Spoken example, first person

HttpClient is a trap because the obvious code is wrong. If I create one in a using block for each request, disposing it closes the connection, and the socket sits in TIME_WAIT for a while afterwards. Under load you run out of sockets. The natural fix is a single static client, which solves that but creates a quieter problem: connections are pooled and reused indefinitely, so if a downstream service changes its DNS during a failover, you keep talking to the old address. The factory handles both. It pools the underlying message handlers and recycles them periodically, so connections are reused but DNS changes get picked up. It also gives me one place for configuration. I use typed clients, so a payments client class gets its base address, timeout and headers injected, and retry and circuit breaker policies live there too. Where there is no dependency injection, a static client with a pooled connection lifetime set on the handler achieves the same thing, and that is a perfectly valid choice.

Walking into this interview soon? GhostPilot listens to your live call, spots the question the moment it is asked, and puts a structured answer on your screen in real time. Try it on your next mock, or grab a $29 Session Pass, no subscription, for the real thing.

See how it works

Follow-up questions to expect

  • What is the difference between a named client and a typed client?
  • Why should you be careful capturing a typed client inside a singleton?
  • How would you add retries without retrying requests that are not safe to repeat?

Related .net developer questions

Your interviewer will ask their own version of this. Paste your actual job description into the free Question Predictor and get the 20 questions that role is most likely to ask, with what each one is really probing.

Predict my questions

Rehearse the hard questions before they are asked

Practise with a live copilot, then walk in ready. A $29 Session Pass gets you through the interview with no subscription and no lock-in.

Get GhostPilot