Sunday 24 January 2021

Part 9: Launch Setting and App setting Json File

#20 minutes reading 


LaunchSetting.Json and Appsettings.json file are used for configuring values required for application.

 

LaunchSetting File:

As name suggest launch setting is used to configure application launch settings. This file contains application launch related configuration and used only for development environment and this file not included in production/staging environment. Asp.Net Core 3.1 provide default configuration for launchsettings.json file while creating project.

 

I’m using previous article project created for Part 8 (UseDeveloperException MiddleWare article).


By default, Asp.Net Core 3.1 provide above piece of code in launchSettings.Json file highlighted above.

Launch setting provides two profile IIS express and ExceptionPage (project name), based on hosting model profile gets chosen and executed. Either from IIS express or from ketrel.

Setting available in this path is actually used for launch application from visual studio or .Net CLI and which server needs to use either IIS Express or kestrel.

 


When executing application with IIS option, IIS express is used shown below and it used IIS express process name which we already discussed in In-Process hosting model. 



When executing application with Exceptionpage option, kestrel is used to launch application with process as project name (Exception Page).  

Again, if you notice in each profile contains environmentVariable and set to Development. 



This environment variable referred in configure method of Startup.cs file to check environment and if environment is development generates details exception using UseDeveloperException middleware.



AppSettings Files:

This is same as web.config file from old MVC .Net framework. It stores values which are common in application and used anywhere like connection string, path of log file, ...etc.

 

Again, I’m using same application. Open appSettings.json.

 

Add below highlighted lines of code in appsettings.json. 


IConfiguration class file responsible for reading configuration values in Asp.Net Core application available in namespace Microsoft.Extensions.Configuration.

Open startup.cs file and inject IConfiguration service in constructor of startup class and read connection string property in Configure method of startup class shown below.

 


Run application and check browser window.

 

Appsettings.json file can be created based on environment (Development, Statging or Production).

Appsettings.{environment}.json. environment can be Development, Staging or Production.

Appsettings.{environment}.json file override property of Appsettings.json if both file contains same configured property name. lets understand with example.

For now environment variable set as Developer in launchsettings.json but environment variable can be set from other sources also for staging and production will cover in future article.



Appsettings.Development.json file default available with project, we can create for staging and production. Lets add ConnectionStringProperty in appsettings.development.json file.



Same property available on appsettings.json also, now check which value showing on browser.

It means if both appsettings.json and appsettings.development.json file share same property name in that case value from appsettings.development.json file value gets priority. 

Now lets discuss why Asp.Net Core provides below three different environment properties.

        ·         Appsettings.development.json

        ·         Appsettings.staging.json

        ·         Appsettings.production.json

 

Connection string, file path and few more configuration value different for all three environments so needs to modify values for each environment difficult after publishing application to respective environment.To resolve this issue create staging and production environment configured value for respective environment.

 

As launchsetting.json file is for development purpose so need to configure environment variable using system variable from control panel. Create environment variable for staging and production on respective machine. Follow below steps for creating environment variable.

 

Go to control panel and under system click on below highlighted option (edit the system environment variables)


Click on Environment Variables button and it will open Environment window.


From environment window click on New button. 


 

How we can retrieve environment variable we already discussed under startup.cs file.

 


Above highlighted return true if environment is development. IsStaging and IsProduction also work same way and return Boolean value based on environment variable set for OS.


Previous                                                                                                                                                Next


0 Comments:

Post a Comment

Subscribe to Post Comments [Atom]

<< Home