C# (.NET) Application Configuration: Libraries and Tools

Online Python Trainer for Beginners

Learn Python easily without overwhelming theory. Solve practical tasks with automatic checking, get hints in Russian, and write code directly in your browser — no installation required.

Start Course

C# (.NET) Application Configuration: Complete Guide



Configuration is one of the key aspects of developing modern C# (.NET) applications. It allows you to separate program parameters from code, making the application flexible, portable, and secure. In this article, we will thoroughly examine the Microsoft.Extensions.Configuration library — the main tool for managing configuration in .NET Core and .NET 5/6/7/8+.



Why is configuration needed?

Instead of hardcoded connection strings, URLs, or API keys, configuration allows you to store this data in files (JSON, XML, INI), environment variables, or secrets. This simplifies deployment across different environments (dev, staging, prod) and improves security.



Main components of the library

  • IConfiguration — an interface representing a unified settings tree.
  • IConfigurationBuilder — a builder that collects configuration from various sources.
  • ConfigurationRoot — the root object implementing IConfiguration.
  • Source Providers — data providers: JSON, XML, INI, Environment Variables, Command Line, Memory, Custom.


Installing the library

The library is included in the .NET SDK, so for most projects no additional installation is required. However, to work with JSON files in console applications, you need to add the package:

dotnet add package Microsoft.Extensions.Configuration.Json


Basic example: reading from appsettings.json

Let's create a file appsettings.json:

{  "AppSettings": {    "SiteName": "PythonLib",    "Version": "1.0",    "Logging": {      "Level": "Information",      "File": "logs/app.log"    }  }}


Now let's read it in a console application:

using Microsoft.Extensions.Configuration;

class Program{ static void Main() { var builder = new ConfigurationBuilder() .SetBasePath(Directory.GetCurrentDirectory()) .AddJsonFile("appsettings.json", optional: false, reloadOnChange: true);

IConfiguration config = builder.Build();

string siteName = config["AppSettings:SiteName"]; string logLevel = config["AppSettings:Logging:Level"];

Console.WriteLine($"Site: {siteName}, Log Level: {logLevel}"); }}


Using multiple sources

One of the main advantages of Microsoft.Extensions.Configuration is support for source chains. The last added source has the highest priority. A typical chain:

  1. appsettings.json — base settings.
  2. appsettings.{Environment}.json — settings for a specific environment (Development, Production).
  3. Environment Variables — override for CI/CD and Docker.
  4. Command Line Arguments — for temporary overrides at startup.


Example of setting up a chain:

var builder = new ConfigurationBuilder()    .SetBasePath(Directory.GetCurrentDirectory())    .AddJsonFile("appsettings.json", optional: false, reloadOnChange: true)    .AddJsonFile($"appsettings.{Environment.GetEnvironmentVariable("ASPNETCORE_ENVIRONMENT") ?? "Production"}.json", optional: true)    .AddEnvironmentVariables()    .AddCommandLine(args);


Working with sections and strongly typed configuration

For convenience, you can bind configuration sections to POCO classes. Let's define a class:

public class AppSettings{    public string SiteName { get; set; }    public string Version { get; set; }    public LoggingSettings Logging { get; set; }}

public class LoggingSettings{ public string Level { get; set; } public string File

Recommendations