Using IConfiguration Inside Console App .NET Core

Jin Vincent Necesario
3 min readApr 2, 2023

--

Photo by Malte Helmhold on Unsplash

Introduction

Usually, dot net developers from an ASP.NET Web API background know that the IConfigurationis available by default. However, encountering the Console Application, they scratch their heads about using the IConfiguration.

This article will explore accessing and using IConfiguration on a Console Application. OK, let’s get started.

Background

The truth is .NET Core’s Console Applications don’t have dependency injection by default. However, in an ASP.NET .NET Core Web Application, or any related web project, the configuration is available.

We can add the configuration using the ConfigurationBuilderclass. Of course, we can’t ignore the JSON file, environment variables, or command line or use a custom configuration provider.

Here are the Nuget packages we need to install into our Console App to get the expected functionality.

  • Microsoft.Extensions.Configuration
  • Microsoft.Extensions.Configuration.Json
  • Microsoft.Extensions.Configuration.CommandLine
  • Microsoft.Extensions.Configuration.Binder
  • Microsoft.Extensions.Configuration.EnvironmentVariables

However, if you think you’ll be comfortable adding the package reference inside your .csproj file project. Just copy and paste the code below.

Note: Change the version to suit your needs.

 <ItemGroup>
<PackageReference Include="Microsoft.Extensions.Configuration" Version="8.0.0-preview.2.23128.3" />
<PackageReference Include="Microsoft.Extensions.Configuration.Binder" Version="8.0.0-preview.2.23128.3" />
<PackageReference Include="Microsoft.Extensions.Configuration.CommandLine" Version="8.0.0-preview.2.23128.3" />
<PackageReference Include="Microsoft.Extensions.Configuration.EnvironmentVariables" Version="8.0.0-preview.2.23128.3" />
<PackageReference Include="Microsoft.Extensions.Configuration.Json" Version="8.0.0-preview.2.23128.3" />
</ItemGroup>

Installing the Nuget Packages

Before we get into the coding part, install all of the required Nuget packages.

Just copy and paste the code below, and don’t forget to create an appsettings.json file and set the copy to the output directory to copy always in case you forgot.

appsettings.json copy always

Let’s Write the Code

Right after that, you can copy and paste the code below inside the Program.cs file.


IConfiguration configuration = new ConfigurationBuilder()
.SetBasePath(Directory.GetCurrentDirectory())
.AddJsonFile("appsettings.json", optional:true , reloadOnChange:true)
.AddEnvironmentVariables()
.AddCommandLine(args)
.Build();

Now, let’s try to create a simple section inside our appsettings.json.

Let’s call it ServerConnection.

{
"Logging": {
"LogLevel": {
"Default": "Debug",
"System": "Information",
"Microsoft": "Information"
}
},
"ServerConnection": {
"Url" : "localhost",
"Port": "8080",
"IsSSL": "false",
"Password": "helloP@ssw0rd"
}
}

Let’s try to get the Url, Port, IsSSL, and Passwordvalues first.

var serverConnection1 = configuration.GetSection("ServerConnection");

var url = serverConnection1.GetValue<string>("Url");
var port = serverConnection1.GetValue<int>("Port");
var isSsl = serverConnection1.GetValue<bool>("IsSSL");
var password = serverConnection1.GetValue<string>("Password");

Let me show below how it went when debugging through the code.

Nice one; we got those values by using the IConfiguration .

Now, how about binding the ServerConnection config in a class.

Let’s try to see what’s inside the ServerConnection class.

public class ServerConnection
{
public string Url { get; set; }
public string Password { get; set; }
public int Port { get; set; }
public bool IsSSL { get; set; }

public override string ToString()
{
return $"${this.Url}:${this.Port},password=${this.Password},ssl=${this.IsSSL}";
}
}

The code above will represent the ServerConnection appsettings.json. Moreover, we have overridden the ToString method for some debugging purposes.

var serverConnection = new ServerConnection();
configuration.GetSection("ServerConnection").Bind(serverConnection);

Console.WriteLine(serverConnection);

For the output, here’s what you’ll expect.

$localhost:$8080,password=$helloP@ssw0rd,ssl=$False

Let’s see a sample debugging session below.

For the complete source code on the Program.cs here you go.

using ConsoleAppConfig;
using Microsoft.Extensions.Configuration;

IConfiguration configuration = new ConfigurationBuilder()
.SetBasePath(Directory.GetCurrentDirectory())
.AddJsonFile("appsettings.json", optional:true , reloadOnChange:true)
.AddEnvironmentVariables()
.AddCommandLine(args)
.Build();

var serverConnection1 = configuration.GetSection("ServerConnection");

var url = serverConnection1.GetValue<string>("Url");
var port = serverConnection1.GetValue<int>("Port");
var isSsl = serverConnection1.GetValue<bool>("IsSSL");
var password = serverConnection1.GetValue<string>("Password");

var serverConnection = new ServerConnection();
configuration.GetSection("ServerConnection").Bind(serverConnection);

Console.WriteLine(serverConnection);

Console.Read();

Summary

We have seen the NuGet packages needed for a Console App to run and use the IConfiguration to get those settings on the appsettings.json.

Also, we have seen how we can get those values and how we can bind the values into a class.

That’s all for now, guys. I hope you have enjoyed this article.

You can also find the sample code here on GitHub.

Till next time, happy programming!

--

--

Jin Vincent Necesario
Jin Vincent Necesario

Written by Jin Vincent Necesario

https://www.jinoncode.dev/ - For sharing software engineering knowledge, see you there!

No responses yet