Build a resilient C# HTTP client

8 steps · about 4 h 35 min · Intermediate

  1. Plan connection reuse (reference, Microsoft Learn, 35 min)

    Avoid socket exhaustion and stale DNS by choosing an appropriate client lifetime.

  2. Keep I/O asynchronous (reference, Microsoft Learn, 35 min)

    Compose tasks without blocking threads or hiding failures in fire-and-forget calls.

  3. Propagate cancellation (reference, Microsoft Learn, 30 min)

    Let callers stop requests and related parsing work through one explicit signal.

  4. Set a request time budget (reference, Microsoft Learn, 25 min)

    Bound total waiting time and distinguish timeout policy from user cancellation.

  5. Validate HTTP responses (reference, Microsoft Learn, 35 min)

    Handle status, headers and content explicitly instead of assuming every completed request succeeded.

  6. Retry only transient failures (guide, Microsoft Learn, 40 min)

    Use bounded delays and avoid retrying unsafe operations or permanent client errors.

  7. Create a typed client (reference, Microsoft Learn, 35 min)

    Centralise configuration and dependencies behind a small domain-focused interface.

  8. Test through a message handler (reference, Microsoft Learn, 40 min)

    Simulate response sequences and delays without depending on a live external service.