2

After reading this nice post about reading application setting and config from appsettings.json file, I'm little confused about how many times appsettings.json will be read?

This is The Startup class and when the application has been started to work read it for the first time(I guess, If I'm wrong please correct me).

public class Startup
{  
    public IConfiguration Configuration { get; }

    public Startup(IConfiguration configuration)
    {
        Configuration = configuration;
    }
 ...

on the controller:

public class HomeController : Controller
{
   private readonly IConfiguration _config;
   public HomeController(IConfiguration config)
    {
        _config = config;
    }
  ...

The question is that appsettings.json will be read for once time on the Start class or every time we use HomeController it will be read?

Because it's a file and I'm asking about the Reading counts from a physically file on the HDD.

motevalizadeh
  • 5,244
  • 14
  • 61
  • 108
  • 1
    It depends. The json file will be read once at startup when populating `IConfiguration`. However if the config is setup to refresh on change, then it will be reread if changes are made during runtime. – Nkosi Feb 23 '20 at 22:28
  • Every time IConfiguration is injected, the JSON file is not reread. – Nkosi Feb 23 '20 at 22:29
  • 1
    Reference [Configuration in ASP.NET Core](https://learn.microsoft.com/en-us/aspnet/core/fundamentals/configuration) – Nkosi Feb 23 '20 at 22:30
  • 1
    That said. Ideally you should not be injecting `IConfiguration` all over the place. It is at times seen as a code smell. The Options pattern is usually the advised approach. Reference [Options pattern in ASP.NET Core](https://learn.microsoft.com/en-us/aspnet/core/fundamentals/configuration/options) – Nkosi Feb 23 '20 at 22:32
  • This specific location in the docs should be of interest as is focuses on how many times AddJsonFile is called automatically https://learn.microsoft.com/en-us/aspnet/core/fundamentals/configuration/index?view=aspnetcore-3.1#json-configuration-provider – Nkosi Feb 23 '20 at 22:48
  • 1
    @Nkosi You might as well put those comments into an answer because there isn’t anything else to say here :) – poke Feb 24 '20 at 06:16

1 Answers1

2

It depends. The JSON file will be read once at startup when populating IConfiguration.

However if the config is setup to refresh on change, then it will be reread if changes are made to the file during runtime.

Every time IConfiguration is injected, the JSON file is not reread.

Reference Configuration in ASP.NET Core

That said. Ideally you should not be injecting IConfiguration all over the place. It is at times seen as a code smell. The Options pattern is usually the advised approach

Reference Options pattern in ASP.NET Core

This specific location in the docs should be of interest as is focuses on how many times AddJsonFile is called automatically by the framework when setting up configuration

JSON Configuration Provider

Nkosi
  • 235,767
  • 35
  • 427
  • 472