C# – The performance gains of HttpClient reusing connections

When you use the same instance of HttpClient for multiple requests (sequential and concurrent) to the same URL, it’ll reuse connections. Requests that get to reuse a connection are 5.5-8.5x faster than requests that have to open a new connection. There are a few scenarios that benefit from this connection reuse: Sending sequential requests frequently. … Read more

C# – Round up to the nearest 30 minutes

Here’s how to round a DateTime up to the nearest 30 minutes: When the time is 3:38 pm, it rounds to 4:00 pm. When it’s 5:03 pm, it rounds to 5:30 pm. When it’s exactly 2:00 pm, it’ll round up to 2:30 pm (note: see the What if you’re at the start of a 30 … Read more

C# – Disposing the request HttpContent when using HttpClient

Before .NET Core 3.0 (including .NET Framework), HttpClient disposes the request HttpContent object for you. This is surprising default behavior (a violation of the principle of least surprise for sure). This causes multiple problems, but one of the main problems is it prevents you from reusing the HttpContent object (you’re greeted with an ObjectDisposedException if … Read more

C# – How to send a file with HttpClient

In order to send a file in a request with HttpClient, add the file into a MultipartFormDataContent object, and send this object as the request content. Here’s an example: This sends the following multipart/form-data POST request: In this article, I’ll explain a few details about MultipartFormDataContent, and show a few other file-sending scenarios. MultipartFormDataContent Add() … Read more

C# – How to programmatically update the User Secrets file

User Secrets are stored in secrets.json. This file is specific to your application. Once you know the path of secrets.json, you can load and update it. Here’s an example of how to update secrets.json programmatically: Note: 1) For brevity, this isn’t showing all using statements. 2) This is using Newtonsoft because it’s better than System.Text.Json … Read more

ASP.NET Core – How to receive a file in a web API request

When the client posts a file in a multipart/form-data request, it’s loaded into an IFormFile object. This contains file information (such as the file name) and exposes the file content as a stream. This allows you to save the file or process it however you want to. You can access the IFormFile object through Request.Form.Files: … Read more

C# – How to read response headers with HttpClient

When you send a request with HttpClient, it returns an HttpResponseMessage. You can read the response headers through the HttpResponseMessage.Headers property: This outputs the response headers: Raw response headers are really just key/value(s) pairs. When the response comes in, the headers are loaded into the Headers property (which is of type HttpResponseHeaders). This parses the … Read more

EF Core – Inheritance mapping

There are two ways to do inheritance mapping in EF Core: Table-per-Hierarchy (TPH) = There’s a single table for all classes in the hierarchy. Table-per-Type (TPT) = There’s one table per class in the hierarchy. This is available in EF Core 5 and above. Let’s say we have a database with employees. All employees have … Read more

C# – Deserializing JSON with quoted numbers

There are two ways to represent numbers in JSON: as number literals (ex: 123) or as quoted numbers (ex: “123”). In this article, I’ll explain how quoted numbers are handled during deserialization in Newtonsoft and System.Text.Json and how to change the behavior. At the end, I’ll show how to write quoted numbers during serialization. Quoted … Read more

ASP.NET Core – How to return a 500 response

The simplest way to return a 500 response is to use the Problem() helper method, like this: This produces the following response: The ControllerBase class has many helper methods like Problem() that simplify returning responses. These have several optional parameters that you can use to customize the response. Internally, these helper methods return result objects. … Read more