liveBook · Manning

SendAsync

As explained in ASP.NET Core in Action, 3e

SendAsync is the asynchronous HttpMessageHandler method that processes an HTTP request and returns its response.

SendAsync is the asynchronous method that processes an HTTP request and returns its response. In a custom handler, override it to add behavior before forwarding the request through the handler pipeline. section 33.4

A custom handler typically follows this sequence:

  • Derive a handler from DelegatingHandler. section 33.4
  • Override SendAsync with the request and cancellation-token parameters. section 33.4
  • Modify the request, such as by adding an API key header. section 33.4
  • Call base.SendAsync() to continue through the remaining handlers and eventually HttpClientHandler. section 33.5
  • Inspect or modify the response after the downstream handlers finish. section 33.4

Adding an API key

An ApiKeyMessageHandler overrides SendAsync, adds an API-KEY header from injected settings, awaits the downstream response, and returns it. Register the handler with dependency injection, then attach it to a named or typed client with AddHttpMessageHandler<T>().

How ASP.NET Core in Action, 3e covers it

How deep it goes
Explains overriding, request modification, pipeline forwarding, response inspection, registration, and handler ordering.
Examples
Demonstrates an API-key handler that adds a header before forwarding requests through a typed-client pipeline.
What it assumes
Assumes familiarity with HttpClient handlers and dependency injection while establishing the handler pipeline steps explicitly.
What you can do after
Apply it via DelegatingHandler to customize outgoing requests and process responses in an HttpClient pipeline.
How it connects
Connects SendAsync with DelegatingHandler, HttpClientHandler, IHttpClientFactory, dependency injection, and typed or named clients.

Also explained in

Compare how 2 books explain SendAsync →

Related concepts

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