How To
How to unit test async methods in C#
By Tan Lee Published on Jan 22, 2025 291
Unit testing asynchronous methods in C# is an essential skill for ensuring your code functions as expected, especially when working with external dependencies like web APIs or databases.
How to consume an SSE endpoint using HttpClient in C#
By Tan Lee Published on Jan 22, 2025 413
Server-Sent Events (SSE) offer an efficient way for clients to receive real-time updates from a server.
How to send a file using HttpClient in C#
By Tan Lee Published on Jan 22, 2025 413
When working with HTTP requests in C#, one common task is sending files through a POST request.
Sending query strings using HttpClient in C#
By Tan Lee Published on Jan 21, 2025 376
Query strings consist of a ? followed by one or more key-value pairs separated by &. It is essential to ensure that special characters, such as spaces or Unicode characters, are properly encoded.
Performance Boost from Reusing HttpClient Connections
By Tan Lee Published on Jan 21, 2025 215
When you reuse an HttpClient instance for multiple requests, the connection is kept open and reused for each subsequent request.
How to change the HttpClient timeout per request in C#
By Tan Lee Published on Jan 21, 2025 649
When making multiple requests with a shared HttpClient instance, you may sometimes need to modify the timeout for specific requests.
Handling Redirects with HttpClient in C#
By Tan Lee Published on Jan 21, 2025 366
By default, HttpClient handles redirects automatically. When a request results in a 3xx response code (e.g., 301, 302), HttpClient follows the Location header and makes a new request to the provided URL.
How to read problem details JSON using HttpClient in C#
By Tan Lee Published on Jan 21, 2025 594
Problem details (RFC7807) is a standardized format for error responses, which uses the `Content-Type` of `application/problem+json`, an appropriate HTTP status code (e.g., 400 – Bad Request), and a response body structured as follows:
How to Cancel an HttpClient Request in C#
By Tan Lee Published on Jan 21, 2025 230
In C#, you can achieve this by using the CancellationToken with the HttpClient class.
C# Async Main
By Tan Lee Published on Jan 21, 2025 179
The Async Main feature, introduced in C# 7.1, allows you to make the Main() method asynchronous.
The referenced component could not be found
By Tan Lee Published on Jan 21, 2025 308
When opening a C# project in Visual Studio, you may encounter a frustrating error where none of the references load correctly.
Fixing the Sync over Async Antipattern in C#
By Tan Lee Published on Jan 21, 2025 199
One common mistake developers make is the Sync over Async antipattern, which occurs when blocking waits are used in asynchronous methods instead of awaiting them properly.