How To Read The App Settings From Azure In .Net Core

Introduction

 
AppSettings are going to vary from instance to instance, there will be different app settings for the dev environment & different app settings for production environments. If we are deploying the applications to a different customer, app settings will be customer-specific. The best example is if we have an email notification component in your application. To & From emails are usually read from app settings, and these are going to change when the application is deployed to production environments. In these cases, it is important to read the app settings from Azure.
 
In this article, I'm going the explain how to read the app settings from Azure in .NET Core. Let's go step by step.
 
Step 1 - Create Web API Project in .NET Core
 
In Visual Studio, click on File, go to New, and select the appropriate project template.
 
 
Click on "Next", enter the project name and create.
 
 
It creates a project and structure that looks like this.
 
 
In Appsettings.json, add Key value
  1. "TestKey": "I'm Loading from Local App settings"  
Right-click & add a new class called "AppConfig" & interface "IAppConfig". 
  1. public class AppConfig: IAppConfig    
  2.     {    
  3.         public readonly string _testvalue = string.Empty;    
  4.        
  5.         public IConfiguration Configuration { get; }    
  6.         public AppConfig(IConfiguration configuration)    
  7.         {    
  8.             Configuration = configuration;    
  9.             _testvalue = Configuration["TestKey"];    
  10.         }    
  11.     
  12.         public string GetTestValue()    
  13.         {    
  14.             return _testvalue;    
  15.         }    
  16.     }    
  17. public interface IAppConfig    
  18.    {    
  19.       string GetTestValue();    
  20.    }     
Add this dependency to the startup.cs file.
  1. services.AddTransient<IAppConfig, AppConfig>();  
Go to Controller & inject "IAppConfig"  
  1. private readonly IAppConfig _appconfig;  
  2.   
  3.        public WeatherForecastController(IAppConfig appconfig)  
  4.        {  
  5.            _appconfig = appconfig;  
  6.        }  
  7.   
  8.        [HttpGet]  
  9.        public IActionResult Get()  
  10.        {  
  11.            var result = _appconfig.GetTestValue();  
  12.            return Ok(result);  
  13.        }  
If you run this project you will get the below response.
 
Step 2 - Create WebApp in Azure & Deploy 
 
Login to your Azure account, in the home page, we have the "App Services" option, click on that to create a new Azure app service.
 
 
It will take you to create a new app service page, fill in all the details and create.
 
 
It will take few seconds of time to deploy. After deploying, click on the go-to-resources.
 
 
In the left side menu, click on the search box & search for configuration, click on the configuration & click on "New Application Settings", add pair of key-value which we used in our appsettings.json. Click on "save", it might take 5-10 secs to deploy the changes.
 
"TestKey": "I'm Loading from Azure". 
 
 
Download the published profile from azure & publish our .Net Core Web API project. 
 
Step 3 - Run & Test 
 
Localhost

Azure
 
It's working.
 
How to handle if we have nested items in appsettings.json 
 
Add nested items in the appsettings.json & modify the key name in the .cs file. 
  1. "Main": {  
  2.     "Inner""I'm inner value from Local App settings"  
  3.   }  
  1. public AppConfig(IConfiguration configuration)  
  2.         {  
  3.             Configuration = configuration;  
  4.             _testvalue = Configuration["Main:Inner"];  
  5.         }  
Next, Go to Azure & add new app settings in the configuration section of the app service. If it's nested, we need to use ":" as a separator.
 
 

Deploy & Check response


Local Response
 
Azure Response
 
It's working.
 

Summary

 
In this article, I explained how to read the app settings from Azure. Hope it's helpful.