Fetching configuration values based on different environment is required in every application. Let suppose we need to fetch Configuration value in ASP.Net Core for different environments say Dev, QA UAT, Prod etc..
ASP.Net Core provides multiple ways to fetch Configuration value and below are the Different ways
- Using IOptions Pattern
- Using IConfigurationRoot interface
- Using ConfigurationBuilder
ASP.Net Core provides us a great feature to fetch the values from Configuration i.e. appsettings.json file using Options pattern and bind it to the Class object and use it across our Code.
Options patterns provide us great benefit to strongly type our Configuration values which was not present in previous .Net Frameworks
Suppose we need to fetch Employee Configuration from appsettings.json file
"Employee": {
"Id": 1,
"Name": "James",
"Designation": "Software Developer",
"Department": "Sales"
}
We can create an EmployeeOption Class with all the properties same as we have mentioned in Configuration
public class EmployeeOption
{
public int Id { get; set; }
public string Name { get; set; }
public string Designation { get; set; }
public string Department { get; set; }
}
Now we have to mention in ConfigureServices method of Startup.cs file to fetch the values of Configuration when the Application Starts
services.Configure(Configuration.GetSection("Employee"));
Now create a Constructor in Controller Class Injecting IOptions
Create a private variable as well to bind the values of Configuration in it and use it across the Controller
private readonly EmployeeOption _employeeOption;
public EmployeeController(IOptions<EmployeeOption> employeeOptions)
{
_employeeOption = employeeOptions.Value;
}
[HttpGet]
[Route("name")]
public string GetEmployeeName()
{
return _employeeOption.Name;
}
Now while debugging you can see values are getting fetched in the EmployeeOption Class Object
Using IConfigurationRoot Interface
IConfigurationRoot interface exist in Microsoft.Extensions.Configuration namespace and it can be also used to fetch configuration values
Inject IConfigurationRoot interface as a singleton instance in Program.cs files as below
public static IWebHostBuilder CreateWebHostBuilder(string[] args)
{
IConfigurationRoot configurationRoot = null;
return WebHost.CreateDefaultBuilder(args)
.ConfigureAppConfiguration((context, builder) =>
{
configurationRoot = builder.Build();
})
.ConfigureServices((context, services) =>
{
services
.AddSingleton(configurationRoot);
})
.UseStartup<Startup>();
}
Declare and Inject IConfigurationRoot in your Controller class ( It can be any class where you want fetch configuration value in my case it's controller class)
IConfigurationRoot Configuration { get; }
public EmployeeController(IConfigurationRoot configurationRoot)
{
Configuration = configurationRoot;
}
Then Use it like below to get the Configuration value
Configuration.GetValue<string>("Employee:Department");
Using ConfigurationBuilder
Just like we had ConfigurationManager in Legacy .Net Framework similarly we have ConfigurationBuilder in ASP.NET Core to get the configuration value
Below example can be used to fetch value from any configuration file i.e. appsettings.Development.json file, appsettings.Production.json or the root appsettings.json file.
private static string GetConfigurationValue(string key)
{
var environment = Environment.GetEnvironmentVariable("ASPNETCORE_ENVIRONMENT");
var builder = new ConfigurationBuilder()
.SetBasePath(Directory.GetCurrentDirectory())
.AddJsonFile($"appsettings.{environment}.json").Build();
var rootbuilder = new ConfigurationBuilder()
.SetBasePath(Directory.GetCurrentDirectory())
.AddJsonFile($"appsettings.json").Build();
return builder.GetValue<string>(key) != null ? builder.GetValue<string>(key) :
rootbuilder.GetValue<string>(key) != null ? rootbuilder.GetValue<string>(key) : string.Empty;
}
Now you can use the above private method to fetch configuration value
GetConfigurationValue("Employee:Designation");
Hope you have enjoyed the above article.
Share This Post
Support Me