HttpClient Timeout in C#: Default Value and How to Change It

HttpClient timeout card: the default Timeout 00:01:40 changed to 5 seconds, ending in TaskCanceledException after 5.0 seconds

The mistake I see most with a C# HttpClient timeout is expecting a hung API call to fail fast. It won’t: the default Timeout is 100 seconds, and my stuck request on .NET 10 gave up after a full 100 seconds with a TaskCanceledException. The fix is one line, set before the first request: Every … Read more >>

Add a Bearer Token to HttpClient in C# (Header, Handler and Refresh)

C# code that calls an API without an Authorization header and gets 401 Unauthorized, then sets a Bearer token with AuthenticationHeaderValue and gets 200 OK

Your API call keeps coming back 401 Unauthorized, and the token is sitting right there in a variable. HttpClient just isn’t sending it. Quick answer: to send a C# HttpClient bearer token, set the Authorization header to an AuthenticationHeaderValue with the “Bearer” scheme: We tested it against a local .NET 10 API that returns real … Read more >>

HttpClient vs IHttpClientFactory in C#: Avoid Socket Exhaustion

C# code that sends 100 requests with new HttpClient per call and with one shared HttpClient, output showing 100 versus 0 sockets in TIME_WAIT

Use IHttpClientFactory when your app has dependency injection, and one shared HttpClient with PooledConnectionLifetime when it doesn’t. That’s my rule for C# HttpClient vs HttpClientFactory. The one wrong answer is new HttpClient() for every request. I sent 100 requests to a local API on .NET 10. A new client per call opened 100 connections and … Read more >>

System.Text.Json vs Newtonsoft.Json: Which Should You Use in 2026?

System.Text.Json vs Newtonsoft.Json benchmark table on .NET 10: serializing 500 orders took 611.5 us with System.Text.Json and 908.6 us with Newtonsoft.Json

If you’re choosing a JSON library for a new project, use the built-in serializer for anything on .NET 8 or later: System.Text.Json. Keep Newtonsoft.Json when code depends on its loose parsing, TypeNameHandling or JSONPath. That’s my verdict on System.Text.Json vs Newtonsoft.Json in 2026. I ran both on the same 500 US orders with BenchmarkDotNet 0.15.8. … Read more >>

Primary Constructors in C#: Examples, DI and Inheritance

C# primary constructor: the field assignment _customer = customer; from a classic constructor becomes class Order(string customer)

Say you’re writing checkout code for Ridgeline Outfitters, a gear shop in Boise, Idaho. Every class opens with the same chore: declare private fields, write a constructor, copy each argument across. A C# primary constructor cuts that to one line. Put the parameters right after the class name, like class Order(string customer, decimal subtotal), and … Read more >>

Distinct and DistinctBy in C#: Remove Duplicates from a List

C# LINQ Distinct turning the ZIP code list 78701, 80202, 78701, 12207, 80202 into 78701, 80202, 12207

To remove duplicates from a list, call Distinct() and then ToList(). The C# LINQ Distinct method keeps the first copy of each value and drops the rest: For objects, DistinctBy(s => s.Email) (.NET 6+) removes duplicates by one property. I ran every example on .NET 10 against one webinar sign-up list with three hidden duplicates. … Read more >>