Setup Dependency Injection and IConfiguration in a .NET Core 3.1 Console App (Non-host)

Anubhav Gupta
3 min readMay 10, 2021

Introduction

ASP.NET Core Web Application and other templates come with a lot of offerings out of the box but in a blank Console non-host application, one might want to configure these offerings. In this post, we’ll see how to set up the DI container using service collections and get configurations from various sources using IConfiguration.

We’ll add a logging configuration to the application by reading from a JSON file. We’ll configure a Dependency Injection(DI) container and add a logger and a custom class; which takes Logger as a parameter and provides an entry point to the application; to the container. We’ll get an object o the custom class from the DI container; which will automatically resolve the dependencies for the custom class; and used it to execute a function that serves as an entry point to the application.

Setting up IConfiguration

IConfiguration allows the users to read configurations from various sources like JSON files, environment variables, command-line variables, etc, collates them, and allows the user to use these configurations in the application.

We will be setting up IConfigurations to read the configurations from a JSON file. To get the configurations from other sources you may need to add some more relevant NuGet packages.

  1. For our use case, we need to install Microsoft.Extensions.Configuration v3.1.14 and Microsoft.Extensions.Configuration.Json v3.1.14.
  2. Add a JSON file in your project named “appsettings.json” and add the following code to it-

Make sure you select the option to copy the file to the output file. Without this, the file wouldn’t be included in the compiled files and the program wouldn’t be able to find the file. We have kept this file mandatory. If required you can make it optional by setting the optional property to true.

3. Add the following code in the Main function in Program.cs

Setting Up IConfiguration in the main function

This would add the configurations from the JSON file to your application.

Setting Up the DI Container

Using Dependency Injection not only helps in automatically managing the lifecycle and injecting dependencies for the object but also helps to implement inversion of control.

We will create a DI container using a service collection and use it to build the service provider from which we can get objects with their dependencies injected. We, however, need to add all dependencies to the DI container else it’ll throw an exception when it will try to create an object. We will add a simple class to the container with a function that will serve as an entry point to the application. We will also configure the logger and add the Console provider to it. You can add the other relevant Nuget Packages for the other providers you might want to add.

  1. Install the Microsoft.Extensions.DependencyInjection v3.1.14 NuGet package for the DI container and Microsoft.Extensions.Logging v3.1.14 and Microsoft.Extensions.Logging.Console v3.1.14 Nuget packages for the logger
  2. Create a new class called “ExecuteProgram” and add a public Run method to it. This function will serve as the entry point to the application.

3. Add the following code to set up a DI container using service collection and to build the service provider in the main function after the above code.

4. Add any other dependencies you want to add.

5. Add code to starting running the application. In our example, we will start running the application by executing the ‘Run’ function in our “ExecuteProgram” class using the following code.

Summary

We saw how to read the configuration from a JSON file and set up dependency injection(DI) in the application. We added a class called “ExecuteProgram” and Logger in the DI container. We ran the application by executing the Run function in “ExecuteProgram”.

The final program.cs should look like this-

Let me know if you found this useful. I would also appreciate any feedback you can give, good or bad. It’ll help me improve and keep writing more stories.

--

--