ASP.NET – How to use a BackgroundService for long-running and periodic tasks

In ASP.NET, when you need a long-running background task, or need to run a task periodically, you can implement IHostedService or BackgroundService and register your class as a hosted service in Startup. As you may have already encountered, if you try to run a long-running background task, the web server will randomly kill your task … Read more ASP.NET – How to use a BackgroundService for long-running and periodic tasks

ASP.NET – InvalidOperationException: Synchronous operations are disallowed

Problem You’re trying to do synchronous IO operations in ASP.NET and you get the following exception: System.InvalidOperationException: ‘Synchronous operations are disallowed. Call WriteAsync or set AllowSynchronousIO to true The reason you’re getting this is because server option AllowSynchronousIO is false. Starting in ASP.NET Core 3.0, this is false by default. Solution Microsoft turned this setting … Read more ASP.NET – InvalidOperationException: Synchronous operations are disallowed

Event-driven .NET: Concurrent Producer/Consumer using a non-blocking, async queue

In a previous article I wrote about how to implement concurrent producer/consumer using a BlockingCollection. This is a thread-safe, event-driven approach that uses high-level concurrent constructs. The only downside is that the consumer uses a blocking call to dequeue messages. In other words, it wastes a thread. Is there a way to implement this using … Read more Event-driven .NET: Concurrent Producer/Consumer using a non-blocking, async queue

ASP.NET – Async SSE endpoint

Server-Sent Events (SSE) allow a client to subscribe to messages on a server. The server pushes new messages to the client as they happen. This is an alternative to the client polling the server for new messages. In this article I’ll show how to implement the messaging system shown in the diagram below. This uses … Read more ASP.NET – Async SSE endpoint

Event-driven .NET: How to consume an SSE endpoint with HttpClient

Server-Sent Events (SSE) allow a client to subscribe to messages from the server. It creates a one-way stream from the server to the client. When the server has new messages for the client, it writes them to the stream. This is an alternative to the client polling the server for updates. Use the following to … Read more Event-driven .NET: How to consume an SSE endpoint with HttpClient

C# – How to make concurrent requests with HttpClient

The HttpClient class was designed to be used concurrently. It’s thread-safe and can handle multiple requests. You can fire off multiple requests from the same thread and await all of the responses, or fire off requests from multiple threads. No matter what the scenario, HttpClient was built to handle concurrent requests. To use HttpClient effectively … Read more C# – How to make concurrent requests with HttpClient

C# – How to test that your code can handle another culture’s date format

Let’s say you’re using DateTime.Parse() to convert a date string into a DateTime object. By default, DateTime.Parse() uses CultureInfo.CurrentCulture to figure out the date format. The current culture ultimately comes from your OS settings. So when you run this code on a computer that is using the en-US locale, the current culture will automatically default … Read more C# – How to test that your code can handle another culture’s date format

C# – How to unit test async methods

Let’s say you have the following async method you want to test: Here’s how to unit test this: This is awaiting the method you’re testing. To await it, you must make the unit test method return async Task. This example is a bit simplistic. In the real world when you are working with async methods, … Read more C# – How to unit test async methods

C# – Use SemaphoreSlim for throttling threads

When you have multiple threads trying to do work at the same time, and you want to throttle how many of them are actually executing, you can use SemaphoreSlim. Example – a busy grocery store Grocery stores have a limited number of checkout lanes open. Let’s say the grocery store has two lanes open, and … Read more C# – Use SemaphoreSlim for throttling threads