Getting Started With ASP.NET Core - Part Four

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. 
 
Getting Started With ASP.NET Core 
 
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.
 
Getting Started With ASP.NET Core
 
Getting Started With ASP.NET Core 
 

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.
 
Getting Started With ASP.NET Core 
 
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.
  1. namespace WebApplication15  
  2. {  
  3.     public class Startup  
  4.     {  
  5.         private IConfiguration _config;  
  6.   
  7.         public Startup(IConfiguration config)  
  8.         {  
  9.             _config = config;  
  10.         }  
  11.           
  12.         public void ConfigureServices(IServiceCollection services)  
  13.         {  
  14.             services.AddSingleton<IMessages, Message>();  
  15.         }  
  16.   
  17.         public void Configure(IApplicationBuilder app, IHostingEnvironment env)  
  18.         {  
  19.             if (env.IsDevelopment())  
  20.             {  
  21.                 app.UseDeveloperExceptionPage();  
  22.             }  
  23.   
  24.             app.Run(async (context) =>  
  25.             {  
  26.                 await context.Response.WriteAsync(_config["Message"]);  
  27.             });  
  28.         }  
  29.     }  
  30. }  
We have written the below code of lines in green to read the configuration information.
 
Code
 
Getting Started With ASP.NET Core 
 
Output
 
Getting Started With ASP.NET Core 
 

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.
 
Getting Started With ASP.NET Core 
 
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.
 
Getting Started With ASP.NET Core
 

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.
 
Getting Started With ASP.NET Core 
 
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.
 
Getting Started With ASP.NET Core 
 
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.
 
Getting Started With ASP.NET Core 
 

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.
 
Getting Started With ASP.NET Core
 
Getting Started With ASP.NET Core 
 
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.
 
Getting Started With 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.