Load Configuration – INI,XML,JSON in .NET
In this article, we will see how to Load Configuration – INI,XML,JSON in .NET in .NET Core Console or WinForm which are mainly non-host applications.
Further, we will also see how to load configuration from different file providers like JSON or INI, or XML files.
This article covers below File provider’s loading techniques,
- Application Configuration File(apsettings.json)
- Loading (.INI) File configuration
- Loading (XML) File configuration
- Loading any Custom(JSON)
- Loading configuration without Dependency injection
With the expanded support of the .NET Core ecosystem to applications like Console or Windows Form and WPF in the .NET Core Core ecosystem, It is very much possible that you might need to use your traditional Config file system like XML, JSON or INI file, etc.
Today in this article, we will cover below aspects,
You can pretty much read all the above file systems in a simple and easy way without hassle using File Providers available in .NET Core.
If you are interested to load configuration in .NET Core using dependency injection(DI), I have talked about how to load key-value pairs via dependency injection using IConfiguration and IOption interface.
So let’s get started with the .NET Core Console application as an example,
Default Loading of appsettings.json
Loading of appsettings.json support is provided out of the box in .NET Core-based application.
That means you don’t really need to write a single line of code to load these details.
As we discussed in our last article CreateDefaultBuilder() method support to load configuration details provided in appsettings.json or appsettings.{env.EnvironmentName}.json by default.
Example:
I have a .NET Core console application as below.
I shall be using a CreateDefaultBuilder Host builder to set up the dependencies of configuration and business objects.
var hostBuilder = Host.CreateDefaultBuilder()
.UseConsoleLifetime();
Application Configuration File(apsettings.json)
When you create a Console application using the .NET Core project template, it doesn’t add a configuration file by default.
Please add a new Application Configuration file i.e appsettings.json using ‘Add New Item’ section in Visual Studio IDE.
Below is my sample appsettings.json file,
{
"Logging": {
"LogLevel": {
"Default": "Warning"
}
},
"AllowedHosts": "*",
"ServerURL": "https://localhost:44347/api/employee"
}
Please see the article on Implementing DI(Dependency Injection) in the Console.NET Core application for more detail.
I have used CreateDefaultBuilder() to create the host builder.
Class MyApplication implementation is as below. We are using constructor injection for the IConfiguration interface which gives access to Application configuration and also access to environment variables if any.
public class MyApplication
{
private readonly string _baseURL;
public MyApplication(IConfiguration configuration)
{
_baseURL = configuration["ServerURL"];
}
internal void Run()
{
Console.WriteLine(_baseURL);
}
}
Let’s execute the application and verify the configuration details,
Get Logging Configuration
The similar way any logging configuration set up in the apsetting.json file will be available by default for logging purposes. Again CreateDefaultBuilder() will give you this feature out of the box.
Get Environment Configuration
A similar way to any Environment configuration on the host machine will be available by default. Again creating Host Builder using CreateDefaultBuilder() gives you this feature out of the box.
Loading Any Custom JSON file
If you need any custom JSON file to be loaded, please use code to load the configuration.
var hostBuilder = Host.CreateDefaultBuilder()
.ConfigureAppConfiguration((context, builder) =>
{
// Add other configuration files...
builder.AddJsonFile("Custom.json", optional: true);
})
Loading INI and XML Configuration File
You can load configuration details from different file configuration providers like INI or XML files in .NET Core Windows or WPF application easily by using supported providers.
Please refer to the article: Loading Configuration in .NET Core Console or WinForms application- Part II for more details.
Strongly Typed Configuration
One can generate a strongly typed configuration as discussed in the article below,
References: Configuration in ASP.NET Core
Load Configuration using ConfigurationBuilder(without DI)
You can also use Configurationbuilder to load them from the configuration file easily with want to load configuration without IoC.
That’s All. Happy Coding!
Do you have any comments or ideas or any better suggestions to share?
Please sound off your comments below.
Happy Coding !!
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.