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

How to use NLog in ASP.NET

When you want to use NLog in ASP.NET, the first step is to install and configure NLog. Then you can either use NLog directly or fully integrate NLog and ASP.NET. Use NLog directly if you prefer to have static ILogger properties, instead of using dependency injection. The downside of this approach is that you’ll have … Read more

ASP.NET Core – How to make the controllers use Newtonsoft

By default, ASP.NET Core uses System.Text.Json for JSON serialization. If you want to use Newtonsoft instead, you can add the Microsoft.AspNetCore.Mvc.NewtonsoftJson nuget package, then call AddNewtonsoftJson() in Startup.ConfigureServices() like this: In this article, I’ll show how to configure the Newtonsoft serializer options. Install the right nuget package Microsoft packaged up Microsoft.AspNetCore.Mvc.NewtonsoftJson based on the framework … Read more

System.Data.SqlClient is missing in a .NET Core project

When you create a new project in .NET Core, and you try to use a class from ADO.NET, such as SqlConnection, you’ll get missing reference errors. For example, your code might look something like this, and it’ll have red-squiggly lines under SqlConnection: In the past, you’d simply add a reference to System.Data.SqlClient, like this: To … Read more