Configuration in ASP.NET Core
Today in this article, we will see how to Configuration in ASP.NET Core. The configuration gives developers and administrators of applications a way to use required resources (preferably protected) to fulfill the application’s needs.
Let’s look at simple steps and best practices for managing configuration details in .NET Core.
Today in this article, we will cover below aspects,
It’s impossible to see any application without configuration details specified.
.NET Core has simplified the usage of configuration by means of DI (Dependency Injection) or IOC (Inversion of control) principles.
The framework provides better control and flexibility over resources being used in the application.
You can represent your configuration as an object thereby providing type safety to configuration details.
Managing configuration is a broader subject and certainly not in the scope of this article 🙂 so let’s stick to the simple topic of managing and understanding of configuration in ASP.NET Core.
There are even better approaches if you are dealing with Cloud-based applications, where you may want to use server-based or centralized configuration (Enterprise) or want to store secured keys to secret storage.
Examples – ZooKeeper or Config server etc.
DI (Dependency Injection) Configuration Startup.cs file – IConfiguration
I have a simple WebAPI application and has its configuration details as shown below,
Loading appsettings.json
{
"Logging": {
"LogLevel": {
"Default": "Debug",
"System": "Information",
"Microsoft": "Information"
}
},
"CustomerConfig": {
"CustomerKeyurl": "http://customer/key",
"CustomerdetailsUrl": "http://customer/id",
"Agency": {
"AgencyID": "subvalue1_from_json",
"AccountKey": 200
}
},
"ProducerConfig": {
"Key1": "true",
"Key2": "false"
}
}
As I mentioned above there is inbuilt support in .NET Core to provide access to config details. This access is provided by CreateDefaultBuilder() in Program.cs.
Once the application is hosted, config details are actually available via IConfiguration in the app’s Dependency Injection (DI) container.
This Configuration object becomes available anywhere in the application scope using Constructor injection. All config values can be accessed easily in the constructor or another part of the module.
Once you access the Configuration object, you can use the declarative approach to fetch the configuration details.
Example :
var cacheName = _configuration["CustomerConfig:CustomerKeyurl"];
Map configuration to a POCO object
However one may ask the question of why we need this step, but there are many benefits to using this approach. One can load configuration details into strongly typed global classes or can load them into related classes using IOptions.
Once the object is initialized in either way ConfigServices method can be used to inject this service into the pipeline. Once configuration services are enabled, these objects will be used in other parts of the application through the explicit dependency principle.
Here configuration is loaded into the global Configuration object and the same is DI in another part of the application as required.
Example 1
Configuration holds all details from the configuration file.
Example 2
Use GetSection() method to load the required section from the configuration as shown below,
In the above example, Customerconfig and Producerconfig will be available in their respective locations through DI.
Creating Type-Safe Configuration – IOption
Custom class GlobalConfigurationSettings can be created using the ‘Paste Special’ utility within Visual Studio.
Let’s copy the complete appsetting.json content and click on ‘Paste Special’ as shown below in any .cs file opened.
The configuration object is now DI through constructor injection in any
any controller or any classes as shown below,
Here globalConfig object gives access to every element of the settings file.
This pattern gives access to configuration details in related classes and has the choice to load config data based on the need.
Here we will be using the IOption interface to load the configuration as needed in the application.
Example: Customerconfig in CustomerController
Similarly for ProducerConfig,
Please see more details below on loading custom configurations from different sources using a file provider for JSON, INI, and XML files.
Do you have any comments or ideas or any better suggestions to share?
Please sound off your comments below.
Happy Coding !!
Summary
ASP.NET Core pipeline allows us to access configuration details using the IConfiguration interface. The configuration gives developers and administrators of applications a proper way to consume/use required resources. We can create a Type-Safe configuration with the additional benefit of easy maintenance.
Please bookmark this page and share it with your friends. Please Subscribe to the blog to receive notifications on freshly published(2024) best practices and guidelines for software design and development.
.NET core is beauty..lots of goodies. Nicely explained. thanks.
Hey Tom – You made my day !! Sorry for delay in the response. Thanks for your feedback.