C# – Connect to a MySQL database

The simplest way to connect to a MySQL database in a .NET project is to use the MySql.Data package (from Oracle). It provides classes that implement the standard ADO.NET interfaces (such as IDbConnection). First, add the MySql.Data package to your project (this is using View > Other Windows > Package Manager Console): Now use the … Read more

C# – Deserialize JSON to a derived type

The simplest way to deserialize JSON to a derived type is to put the type name in the JSON string. Then during deserialization, match the type name property against a set of known derived types and deserialize to the target type. System.Text.Json doesn’t have this functionality out of the box. That’s because there’s a known … Read more

Use the latest System.Text.Json features in previous framework versions

System.Text.Json is being rapidly developed and they’re always adding new features that you might want (ex: JsonPropertyOrder in v6). The rapid release cycle makes it difficult to keep your established project on the latest framework version. In fact, trying to keep up would probably be detrimental for your project’s health. Fortunately, they made a really … Read more

C# – Convert an object to JSON and vice versa

The simplest way to convert an object to JSON (serialization) is to use the built-in System.Text.Json.JsonSerializer: This serializes the Movie object to JSON using the default serialization settings (notice it’s not pretty printed by defualt). Here’s the JSON this produces: To do the opposite – convert a JSON string to an object (deserialization) – use … Read more

Comparing performance with Benchmark.NET graphs

The following graph compares the execution time of three sort implementations ran against varying input sizes (1k, 10k, 100k): This graph was generated using Benchmark.NET, which I’ll show how to use in this article. I’ll be comparing the performance of multithreaded quicksort implementations (with the non-threaded Array.Sort() as a baseline). Create console app and reference … Read more

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 update appsettings.json programmatically

You have to overwrite the appsettings.json file to be able to update values programmatically. You have to deal with the whole file, not individual parts of it. The process can be summarized in the following steps: Load appsettings.json and deserialize it into an object. Update properties on the object. Serialize the object into a JSON … 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

C# – How to use table-valued parameters (TVP) with ADO.NET, Dapper, and EF Core

Table-valued parameters (TVP) allow you to send multiple rows of data as a parameter into SQL queries. TVPs are flexible. They can be used in both raw SQL queries and with stored procs, and can be used in all types of queries, from inserts to selects. In this article, I’ll show how to use TVPs … 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