How to Read appsettings.json Values in 3 Steps using Dependency Injection in .NET Core
There are various ways to read the configuration settings in the appsettings.json file in .NET Core projects. In this article, I will tell you how to read it with dependency injection.
appsettings.json Configuration File
In .NET projects, centrally collecting and managing configuration settings is critical to the maintenance and extensibility of the application.
Especially in .NET Core projects, configuration settings are stored in the appsettings.json file.
In this article, I will discuss in detail the Dependency Injection (DI) method, which is one of the methods used to read the appsettings.json file.
Dependency injection is a widely used pattern in the world of software development. This pattern allows a class to externally import its dependencies so that the class does not have to create them itself. DI offers great advantages, especially in terms of the testability and reusability of the code. .NET provides a built-in dependency injection framework to implement this pattern. I return to our topic, as DI is a broad topic that needs to be covered in a separate article on its own.
Structure of the appsettings.json file
First, let’s examine the structure of the file. This file keeps the application settings in JSON format.appsettings.json
Below, you will find the appsettings.json file of our sample project.
{
"Logging": {
"LogLevel": {
"Default": "Information",
"Microsoft.AspNetCore": "Warning"
}
},
"ApplicationSettings": {
"DbConnString": "Using DI connection string",
"SubSettings": {
"Name": "Read appsettings.json using DI",
"ApiVersion": 3
}
}
}
This example shows a basic file that contains a database connection string and some logging levels .appsettings.json
Of course, I entered test data as an example.
It is quite easy to read the appsettings.json file with the dependency injection method. I prepared a sample project to implement it.
Below is the folder structure of the project.
Two files in the project are important to us. The first is the appsettings.json file itself and the corresponding class from which we will read the data. I will read the data directly from the Controller class named ReadAppSettingsController.cs.
I chose this approach, which is not very practical in practice, because the project is for educational purposes and to keep the code simple. Otherwise, in order to ensure the code standard, such operations are carried out by a separate service or auxiliary class.
Steps to Read appsettings.json with Dependency Injection
For this, it is necessary to perform the following steps:
Step 1:
IConfiguration is implemented in the class configuration method where we want to read the file appsetting.json the object with DI.
In this way, it will be possible to read the configuration settings individually or as a group by accessing the _configuration object within the class.
Step 2:
It is possible to use the GetValue method to read only one of the configuration settings in the appsettings.json file. Below is the code that allows you to read the value of just one field.
Step 3:
We can also use the following code to read the configuration settings configured in the appsettings.json file as a group.
When we run the project, the Swagger interface greets us.
I have shared the results of each endpoint in order below.
This approach;
I can say that it is a very suitable approach in terms of testability. By using DI, you can easily emulate your interface in unit tests. In this way, you can isolate the controller and test the behavior with different configuration values.
In addition, it can be preferred in terms of offering a flexible solution.IConfiguration
On the other hand, it would be a bit of a complexity increase because it would be necessary to write additional code with DI in each class that needs access to the appsettings.json file.
While minimal in most scenarios, DI can impose a slight performance overhead due to the additional layer of abstraction. However, this is insignificant compared to the benefits that are usually obtained.
Source Code
You can access the source code for my project on Github. I would be very pleased if you gave stars to the project and followed me.
Result
I tried to explain “Reading with Dependency Injection”, which is one of the methods we can use to read appsettings.json file in .NET Core projects.
👏 If you found the content useful, you can follow me and make me happy in this way.
💬 If there is something I overlooked, you can share it in the comments or contact me on my profile.
⬇️ Check out my other articles.
💬 Let me know in the comment section what have I missed? Where I was wrong?
👨👦👦 You can also share this article with your friend. Argue a little on it. It is known that the truth is born in a dispute.
🙃 Stay determined, stay focused, and keep going
Thanks for reading…