다음을 통해 공유


ASP.NET Core: Getting Started (Part 4)

Introduction

This article explains about the Configuration Source, Creating simple services and injecting the services which we have created. Reading the ASP.NET Core previous part of these articles using the below link before reading this article.

Definition

ASP.NET Core is a cross-platform and open-source framework. ASP.NET Core supports Windows, Mac or Linux operation system. It is used to developing with modern, cloud-based and high-performance web applications.

Configuration Source

We are doing many configuration settings in the application while developing, configuration source is used to how to set up the application configuration from different sources. We have to implement  ASP.NET Core application configuration based on the key-value pairs by configuration providers.

Configuration providers read configuration data into key-value pairs from variety of configuration sources. There are different types of configuration sources are available, those are below.

We are doing configuration settings on web.config file in ASP.NET same as in .NET Core we can create the configuration settings in different ways. We are using “appsettings.json” is default configuration source in .NET Core. Configuration settings contain key values pairs. Go to “appsettings.json” we can see some default settings like database connection.

Read Configuration Information

We can read the configuration information using IConfiguration services. IConfiguration is an interface and it is namespace is the “Microsoft.Extensions.Configuration”. The IConfiguration interface looks like below. In-app settings we have added the key as “Message” and give the corresponding value as “Welcome To .Net Core” to the key. We can read the configuration settings using the IConfiguration.

Here going to explain the steps on 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 which we have marked in the screen lines to read the configuration information.

Code

Output

Environment Specific App Settings

Environment specific app settings file such as development, staging, and production app settings files. 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 the 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 the Configuration Source, Creating simple services and injecting the services which we have created. I hope this really helps to new learners, students, and freshers.