This code illustrates the blog article Better timeout handling with HttpClient.
Key features:
- control the timeout per request, rather than globally for all requests
- throw a more sensible exception (
TimeoutException
) when a timeout occurs, instead of the usualOperationCanceledException
On the subject of inheriting from HttpRequestMessage, when the ASP.NET Core documentation discusses delegating handlers here, it says:
HttpRequestMessage.Properties is explicitly named as a recommended way to pass per-request state into the handler, while subclassing HttpRequestMessage is not.