liveBook · Manning

DelegatingHandler

As explained in .NET MAUI in Action

A delegating handler is an HTTP message handler that can inspect or modify a request or response before passing it to another handler.

A DelegatingHandler inspects or modifies HTTP requests before passing them to another handler. Attach one to an HttpClient to apply changes automatically instead of repeating them at every API call. section 8.3.2

Use a delegating handler to apply authentication and other request policies consistently:

  • Inherit from DelegatingHandler and override SendAsync. section 8.3.2
  • Modify the HttpRequestMessage, such as by adding an access-token header, then call the base method. section 8.3.2
  • Register the handler and attach it to a named HttpClient through IHttpClientFactory. section 8.3.3
  • Chain multiple handlers with AddHttpMessageHandler when separate request modifications or a controlled processing order are useful. section 8.3.3

Automatically adding an access token

An AuthHandler can read a stored access token, add it to each request, and pass the request onward. After successful login, assign loginResult.AccessToken to AuthHandler.AuthToken so later authenticated requests contain the token. section 8.3.2

How .NET MAUI in Action covers it

How deep it goes
Explains inheritance, SendAsync, request modification, dependency-injection registration, named clients, and handler chaining.
Examples
Demonstrates MauiStockTake's AuthHandler, AuthService login flow, named authenticated client, and chained request handlers.
What it assumes
Assumes familiarity with HTTP clients and login results while teaching handler inheritance and registration through a concrete authentication task.
What you can do after
Configure an HttpClient that automatically adds authentication headers and composes multiple request policies.
How it connects
Connects DelegatingHandler with HttpClient, HttpRequestMessage, IHttpClientFactory, dependency injection, and access tokens.

Also explained in

Compare how 2 books explain DelegatingHandler →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of .NET MAUI in Action. The section links point to the parts of the book it draws on.