Monday, September 18, 2023

Reading appsettings.json from a C# static method


In this tutorial I will demonstrate an easy way to read configuration settings in appsettings.json from a static C# method. This technique comes in handy when you are building a non-traditional C# application where you cannot use dependency injection to access the configuration object. I will demonstrate my solution with a simple C# console application.

Getting Started

dotnet new console -o ConfigDemo

cd ConfigDemo

We need to install a package to help us read JSON based configuration files:

dotnet add package Microsoft.Extensions.Configuration.Json

In the root folder of your application, create a file name appsettings.json with the following content that specifies a database connection string:


  "ConnectionStrings": {

    "DefaultConnection": "DataSource=foo.db;Cache=Shared;"



When our application gets built and packaged, we want this file to get copied to the output directory. Therefore, we need to add the following XML to the ConfigDemo.csproj file just before the closing </Project> tag.


  <None Include="*.json" CopyToOutputDirectory="PreserveNewest" />


The Code

Let us create a helper class with a static method named GetConfigValue() that reads from the appsettings.json file. Create a C# class named Utils.cs and add to it the following code:

public class Utils {

    public static string GetConfigValue(string config) {

        IConfigurationBuilder builder = new ConfigurationBuilder();

        if (System.IO.File.Exists("appsettings.json"))

            builder.AddJsonFile("appsettings.json", false, true);

        if (System.IO.File.Exists("appsettings.Development.json"))

            builder.AddJsonFile("appsettings.Development.json", false, true);

        IConfigurationRoot root = builder.Build();

        return root[config]!;



The above code first checks appsettings.json for a configuration setting. If it does not find it there then it looks into appsettings.Development.json.

Note that you need to add the following using statement at the top of Utils.cs:

using Microsoft.Extensions.Configuration;

Using our static method

Replace the code in Program.cs with the following:

var connStr = Utils.GetConfigValue("ConnectionStrings:DefaultConnection");

Console.WriteLine($"Connection string: {connStr}");

Run the application. The output should look like this:

Connection string: DataSource=foo.db;Cache=Shared;

Placing database connection strings in appsettings.json is not a good idea. It is best to save it in appsettings.Development.json while making sure that the latter is in your .gitignore so it does not get pushed into source control.

Copy appsettings.json to appsettings.Development.json. Thereafter, delete the following from appsettings.json:

"ConnectionStrings": {

    "DefaultConnection": "DataSource=foo.db;Cache=Shared;"


Run the application again. You should get the same results with the connection string being read from appsettings.Development.json instead of appsettings.json.

I hope this helps in making you an even better C# developer.

No comments:

Post a Comment