Introduction
Previous parts of this series:
Definition
ASP.NET Core is a cross-platform and open-source framework. ASP.NET Core supports Windows, Mac or Linux operating systems. It is used to develop modern, cloud-based and high-performance web applications.
Configuration Source
There are times when an application gets configuration settings from various sources such as a web config file, app config and so on. We can use configuration source for this purpose to configure various sources.
We implement ASP.NET Core application configurations based on the key-value pairs by configuration providers. Configuration providers read configurations data into key-value pairs from variety of configuration sources. There are different types of configuration sources are available, such as Azure Key Vault, Directory Files, App configurations, environment variables, and settings files.
The Web.config file is the common method of storing configuration settings in ASP.NET. In ASP.NET Core, we can create configuration settings in different ways and one of them is using the “appsettings.json”, that is the default configuration source in .NET Core. Configuration settings contain key values pairs.
Go to “appsettings.json” and we can see some default settings like database connection string.
Read Configuration Information
We can read the configuration information using IConfiguration services. IConfiguration is an interface defined in the “Microsoft.Extensions.Configuration” namespace. The IConfiguration interface looks like below.
In app settings, we have added the key as “Message” and gave the corresponding value as “Welcome To .Net Core”. We can read the configuration settings using the IConfiguration.
Here are the steps that explain how to read the configuration settings.
Go to the “Startup.cs” and create the constructor.
Declare the private variable as “_config”and assign the “config” variable to the private variable “_config” using constructor that is called dependency injection. We will see in detail about dependency injection in later articles. We can find the sample code for accessing the configuration settings.
- namespace WebApplication15
- {
- public class Startup
- {
- private IConfiguration _config;
-
- public Startup(IConfiguration config)
- {
- _config = config;
- }
-
- public void ConfigureServices(IServiceCollection services)
- {
- services.AddSingleton<IMessages, Message>();
- }
-
- public void Configure(IApplicationBuilder app, IHostingEnvironment env)
- {
- if (env.IsDevelopment())
- {
- app.UseDeveloperExceptionPage();
- }
-
- app.Run(async (context) =>
- {
- await context.Response.WriteAsync(_config["Message"]);
- });
- }
- }
- }
We have written the below code of lines in green to read the configuration information.
Code
Output
Environment Specific App Settings
Environment specific in-app settings files are that are used in different environments such as Dev, staging, and production.
We have “appsettings.Development.json” files and we have added the same key in two app settings files but environment-specific app settings file override the key values in normal app settings file.
For example, in-app settings, we have the same key-value looks like below screenshot.
When we run the application, “Message” key’s value override by the environment-specific appsettings.json in normal app settings. We can get the output looks like below.
Environment Variable in Launch Settings
Launch Settings is described by all the necessary settings to launch the application. Launch settings contain default settings looks like below screenshot.
Mainly it contains the application URL, application environment variables, IIS settings, and profiles. We can add the environment variable here also. We have added the “Message” key-value pairs in the inside the “profiles” in the launchSettings.json.
We have added the same key-value pairs in the launchSettings.json and appsettings.Development.json (appsettings.{Environment}.json) but launchSettings override the key-value pairs in appsettings.{Environment}.json so we will get the output looks like below.
Command-line Arguments
We can pass the values to key values (“Message”) via command-line arguments. The command-line argument’s key-value pair overrides the launchSettting’s key-value pair. We can see an example below.
Enter below command in command prompt as “dotnet run Message=" Value From Command Line"” and the copy showed URL and paste in the browser then we can see the output.
IConfiguration Services reads the configuration settings from configuration sources files such as appsettings.json, appsettings.{Environment}.json, Environment variables, and Command-line arguments. appsettings.{Environment}.json overrides the appsettings.json’s key-value pairs, Environment variables overrides the appsettings.{Environment}.json’s key-value pairs and Command-line argument override environment variable’s key-value pairs.
Find the below priority order of configuration sources in ASP.NET core.
Conclusion
This article explained various configuration sources, creating simple services and injecting the services which we have created. I hope this really helps to new learners, students, and freshers.