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
Why not just inherit from HttpRequestMessage and add a Timeout property that can be set via the constructor? Then check if the HttpRequestMessage is of that derived type and grab the timeout from there.