DEV Community

Cover image for Best practice for Reading Configuration in .Net Core
.Net Labs
.Net Labs

Posted on

Best practice for Reading Configuration in .Net Core

Every application start with some configurations at it is required to load configurations at time of bootstrap of application.

Most of time we provide string values in appsetting and we use directly in application, let’s learn about different ways to Read configurations

Reading using IConfiguration

Consider below appsetings.json.

"ConfigKeySetting": {
   "Key1": "Keyvalue",
   "Key2": "Keyvalu2",
   "Key3": "Keyvalu2"
 }
Enter fullscreen mode Exit fullscreen mode

When we run application we found all configuration already loaded by Microsoft.AspNetCore.Builder.WebApplicationBuilder

Image description

We are injecting IConfiguration object and we can read all keys values.

 [ApiController]
    [Route("[controller]")]
    public class WeatherForecastController : ControllerBase
    {
        private readonly IConfiguration _configuration;

        public WeatherForecastController(IConfiguration configuration)
        {

            _configuration = configuration;
        }

        [HttpGet(Name = "ReadConnection")]
        public IActionResult Get()
        {
            var key1 = _configuration.GetValue<string>("ConfigKeySetting:Key1");
            var key2 = _configuration.GetValue<string>("ConfigKeySetting:Key2");
            var key3 = _configuration.GetValue<string>("ConfigKeySetting:Key3");

            return Ok(new
            {
                Key1 = key1,
                Key2 = key2,
                Key3 = key3,

            });

        }
Enter fullscreen mode Exit fullscreen mode

When we run application and call API we can see we able to read all keys

Image description

Problem with this approach

a. Default Value Handling : Most of time we put all keys into Azure keyvalue and read from there most of time devops person manages all key vaults so in case my mistake if any key get replaced by devops team then application will break because we do not have any default values if key not found.

We can handle this situation with Options Pattern

b. Real Time Read: Most of time we put all keys into Azure keyvalue and read from there most of time devops person manages all key vaults so in case if we update anything from there then it will not possible to get updated keys without restarting application because everything loaded.

We can read on real time using Options Pattern

b.** No Validation**: IConfiguration does not perform any validation over the configuration values, which might be fatal during the application runtime.

d.** No Type-Safety:** As mentioned earlier, the interface reads configurations as strings that have to be parsed manually. This also increases the chances of configuration-related issues.

**IConfiguration **is a simple and lightweight approach for loading configurations in ASP.NET Core applications from the appsettings file. However, we need a more robust solution for handling a bit more advanced requirements like Validations, Type-Safety, and Reloading etc

Let's learn about Option pattern

Option Pattern

Options Pattern is used to bind a section of configuration settings to the strongly types options classes and add it to the Asp.Net Core Dependency Injection Service Container as singleton lifetime.

Please click here to see complete tutorial

Top comments (0)