liveBook · Manning

Chapter 33: Calling remote APIs with IHttpClientFactory

What you will learn in chapter 33 of ASP.NET Core in Action, 3e

Introduces consuming remote HTTP APIs in ASP.NET Core and previews IHttpClientFactory, resilience policies, and custom handlers for reliable HTTP calls.

After this chapter you will be able to

What it builds on

Section by section

Introduction

Introduces consuming remote HTTP APIs in ASP.NET Core and previews IHttpClientFactory, resilience policies, and custom handlers for reliable HTTP calls.

Key terms: HTTP APIs, HttpClient, third-party services, remote APIs, IHttpClientFactory, HttpClientHandler, handler pipeline, named clients, typed clients, transient errors

33.1 Calling HTTP APIs: The problem with HttpClient

This section explains why creating and disposing HttpClient per request causes socket exhaustion, while keeping one singleton client can miss DNS changes, motivating IHttpClientFactory.

Key terms: HttpClient, HTTP API, RESTful HTTP services, IDisposable, using statement, socket, socket exhaustion, TIME_WAIT, singleton HttpClient, DNS

33.2 Creating HttpClients with IHttpClientFactory

This section introduces IHttpClientFactory as the safer way to create and configure HttpClient instances, previewing default, named, and typed client patterns.

Key terms: IHttpClientFactory, HttpClient, HttpClientHandler, middleware pipeline, CreateClient(), named clients, typed clients, third-party API

33.2.1 Using IHttpClientFactory to manage HttpClientHandler lifetime

This section explains how IHttpClientFactory separates HttpClient from its handler pipeline, pooling and rotating handlers to avoid socket exhaustion and respect DNS changes.

Key terms: HttpClient, HttpMessageHandler, HttpClientHandler, handler pipeline, outbound pipeline, IHttpClientFactory, socket exhaustion, TIME_WAIT, DNS rotation, handler pooling

33.2.2 Configuring named clients at registration time

This section shows how named IHttpClientFactory clients centralize reusable HttpClient configuration and are created by their case-sensitive names.

Key terms: Named Client pattern, IHttpClientFactory, HttpClient, BaseAddress, default headers, configuration function, CreateClient, rates, unconfigured client, case-sensitive

33.2.3 Using typed clients to encapsulate HTTP calls

This section shows how typed clients use IHttpClientFactory and dependency injection to encapsulate calls and configuration for a remote HTTP API.

Key terms: typed client, HttpClient, ExchangeRatesClient, GetLatestRates(), AddHttpClient<ExchangeRatesClient>, IHttpClientFactory, transient service, pooled HttpClient, DI, ConfigureServices

33.3 Handling transient HTTP errors with Polly

This section shows how to add Polly-based transient-error policies to IHttpClientFactory clients so failed remote HTTP calls can be retried through the handler pipeline.

Key terms: transient errors, remote API, IHttpClientFactory, HttpClient handler pipeline, Microsoft.Extensions.Http.Polly, Polly, HttpMessageHandler, named client, typed client, handler pipeline

33.4 Creating a custom HttpMessageHandler

This section shows how to create, register, and attach a custom DelegatingHandler so every request from a named or typed HttpClient can be modified automatically.

Key terms: HttpMessageHandler, DelegatingHandler, HttpClientHandler, HttpRequestMessage, handler pipeline, SendAsync, base.SendAsync, AddHttpMessageHandler<T>, IHttpClientBuilder, singleton

33.5 Summary

This summary recaps how IHttpClientFactory safely creates and configures HttpClient instances, adds resilience policies, and extends calls with custom handlers.

Where this is used later

Concepts explained in this chapter

← Chapter 32: Building custom MVC and Razor Pages components Chapter 34: Building background tasks and ser vices →

These guides are generated from the text of Manning books by Manning's knowledge graph and link to the sections they draw on. The full books are available on liveBook and at manning.com.