C# – Circuit breaker with Polly

In an electrical system, a circuit breaker detects electrical problems and opens the circuit, which blocks electricity from flowing. To get electricity flowing again, you have to close the circuit. The same approach can be implemented in software when you’re sending requests to an external service. This is especially important when you’re sending lots of … Read more

C# – How to use Polly to do retries

Whenever you’re dealing with code that can run into transient errors, it’s a good idea to implement retries. Transient errors, by definition, are temporary and subsequent attempts should succeed. When you retry with a delay, it means you think the the transient error will go away by itself after a short period of time. When … Read more

How to do retries in EF Core

EF Core has built-in retry functionality. To use it, you can call options.EnableRetryOnFailure(), like this: The retry logic is contained in execution strategy classes. The above code is using the default execution strategy class (SqlServerRetryingExecutionStrategy). When you execute a query, it goes through the execution strategy class. It executes the query and checks for transient … Read more

ASP.NET Core – Log messages to the database in the background

I was reading about logging in ASP.NET when I came across this statement about logging to the database: When logging to SQL Server, don’t do so directly. Instead, add log messages to an in-memory queue and have a background worker dequeue and insert data to SQL Server. Paraphrased from https://docs.microsoft.com/en-us/aspnet/core/fundamentals/logging/?view=aspnetcore-5.0#no-asynchronous-logger-methods In this article, I’ll show … Read more

C# – How to batch read with Threading.ChannelReader

In a consumer/producer scenario, there are many reasons why you might want the consumer to read a batch of items. Maybe you’re bulk inserting items into SQL Server, or sending a payload with HttpClient. Sending lots of individual items over the network can be costly, and waiting for a full batch of items before sending … Read more

ASP.NET Core – How to change the JSON serialization settings

System.Text.Json is the default JSON serializer in ASP.NET Core. It uses the following default serialization settings: To change the settings at the service level for all controllers, call AddJsonOptions() in Startup.ConfigureServices() like this: Note: This example is passing in JsonStringEnumConverter, which makes JsonSerializer use the enum name instead of enum value. When you change the … Read more

WinForms: How to handle DataGridViewButtonColumn click event

There’s not a direct way to handle the DataGridViewButtonColumn button click event. Instead, you have to: Handle the DataGridView.CellContentClick event. Check if they clicked the button column. Execute the button click handling logic, passing in the bound data item. In this article I’ll show a step-by-step example of how to handle the button click. Example … Read more

C# – Pass in a Func to override behavior

If I want to change the behavior of a method from the outside, I can pass in a function pointer. This approach exists in every language, and is one way to implement the Strategy Pattern. In C#, function pointers are referred to as delegates, and the two most common ones are Action and Func. The … Read more

Event-driven .NET: Concurrent Producer/Consumer using BlockingCollection

With the Producer/Consumer pattern you have one or more threads producing new work and enqueuing it, and one or more threads consuming that work by dequeuing it and processing it. The consumers and producers share access to the work queue. Think of it like the Post Office. You have one or more people (producers) dropping … Read more

Event-driven .NET: How to add an event to a class

In the previous article we saw how events work. In this article I’ll show how to add an event to a class. In order to illustrate this I’ve created a simple chat app that uses an in-memory message queue. Each chat window is subscribed to the message queue’s OnMessageReceived event, and updates their UI upon … Read more