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
In .NET 5.0, Properties is marked as Obsolete as well in favor of the Options property
Should be noted, in .NET 5.0, the thrown exception carries an InnerException of TimeoutException. If you need per request, I think you still end up with something like this though.