DelegatingHandler is a HttpMessageHandler base class for adding behavior around outgoing HTTP requests. It forwards processing to the next handler and can inspect or modify responses. section 33.4 section 33.5
Create and attach a custom handler in these steps:
- Derive a handler from DelegatingHandler and override SendAsync(). section 33.4
- Add the custom behavior, call base.SendAsync(), and return or modify the response. section 33.4
- Register the handler with dependency injection and attach it using AddHttpMessageHandler<T>() on a named or typed client. section 33.4
- Choose transient or singleton registration according to whether the handler requires state, and use registration order to control pipeline order. section 33.4
Adding an API key
An ApiKeyMessageHandler can inject API-key settings, add an API-KEY header to every outgoing request, and call the next handler. Register it as a transient service, then add it to an ExchangeRatesClient alongside a transient error handler. section 33.4
Figure 33.6 You can use a custom HttpMessageHandler to modify requests before they’re sent to third-party APIs. Every request passes through the custom handler before the final handler (the HttpClientHandler) sends the request to the HTTP API. After the response is received, each handler gets a chance to inspect and modify the response.
Follow the request from a custom handler through the pipeline to HttpClientHandler, then follow the response back through handlers. section 33.4

