C# – ConfigurationSection.Get() returns null

When you use ConfigurationSection.Get() to load an object from appsettings.json, it returns null if the section doesn’t exist. Since you’re probably not expecting this to be null, this can lead to problems surfacing in unexpected places, such as getting a NullReferenceException: Note: If you’re using ASP.NET Core, you’ll be referring to the config via builder.Configuration … Read more

ConfigurationBuilder SetBasePath() / AddJsonFile() are missing

If you’re trying to use ConfigurationBuilder to read from appsettings.json, you probably have the following code snippet and are running into compiler errors: This has three different errors, but the compiler only shows you one error at a time. This is due to the way this is designed – ConfigurationBuilder is in one library and … Read more

C# – How to programmatically update the User Secrets file

User Secrets are stored in secrets.json. This file is specific to your application. Once you know the path of secrets.json, you can load and update it. Here’s an example of how to update secrets.json programmatically: Note: 1) For brevity, this isn’t showing all using statements. 2) This is using Newtonsoft because it’s better than System.Text.Json … 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: There are two options for deserialization. You can either 1) Deserialize appsettings.json into a dynamic object or 2) Load … Read more

Logging to the database with ASP.NET Core

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 Microsoft – No asynchronous logger methods … Read more

User Secrets not working due to missing UserSecretsIdAttribute

Problem You’ve configured user secrets properly, but the framework will not swap in the secret value at runtime. It appears to not be loading secrets.json at all. Check if you’re using GenerateAssemblyInfo=false in your .csproj file. When you add a user secrets file, it generates a user secrets guid and puts it in your .csproj … Read more

EF Core – How to create a database and a table

In this article, I’ll show how to use EF Core to create a database with one table in it. At the end, I’ll show a standalone console app that inserts a record into this database. Note: I’ll be using SQL Server. I haven’t tried this with other database providers. Please leave a comment if you’re … Read more

Feature flags in ASP.NET Core

In this article, I’ll show an example of using a feature flag to toggle an endpoint on/off with a configuration value in appsettings.json. 1 – Install feature management package Install the Microsoft.FeatureManagement.AspNetCore package. Note: This is using (View > Other Windows > Package Manager Console). 2 – Add feature flags to appsettings.json Edit appsettings.json and … Read more

How to add User Secrets in a .NET Core console app

The User Secrets feature in .NET is a safe, simple way to override values in appsettings.json. The overridden values only exist in a file sitting in your own dev environment, so you don’t accidently commit them to your source control repository. This feature is enabled in ASP.NET by default, and the framework does most of … Read more

C# – How to read configuration from appsettings.json

The appsettings.json file is a convenient way to store and retrieve your application’s configuration. You can add it to any project and then use the Microsoft.Extensions.Configuration library to work with it. Since appsettings.json is just a JSON file, you can add any section / values you want (this is easier than working with XML-based app.config … Read more