阅读appsettings.json是否昂贵? [英] Is reading appsettings.json expensive?

查看:56
本文介绍了阅读appsettings.json是否昂贵?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当前,我正在开发一个.net core 2应用程序,该应用程序应根据域的每个请求从appsettings.json中读取一些密钥.(我不得不提到,我有3个共享相同appsettings.json的项目的单一解决方案)我担心每个请求上appsettings.json文件的读取时间.应用启动时是否会缓存此文件?我的启动顾问如下:

Currently I'm developing a .net core 2 application that should read some keys from appsettings.json on each request based on domain. (I have to mention, I have a single solution with 3 projects that share the same appsettings.json) I'm concerned about the reading time of the appsettings.json file on each request. Does this file get cached when the app starts? My Startup consrtuctor is the following :

public IConfiguration Configuration { get; }
private readonly ILoggerFactory _logger;

public Startup(IHostingEnvironment env, ILoggerFactory logger)
        {
            string settingPath = Path.GetFullPath(Path.Combine(@"../SharedConfig/globalAppSettings.json"));
            var builder = new ConfigurationBuilder()
                .SetBasePath(env.ContentRootPath)
                .AddJsonFile(settingPath)
                .AddEnvironmentVariables();

            Configuration = builder.Build();

            _logger = logger;
        }

推荐答案

Configuration API使用 IConfigurationBuilder.Build 方法构造键和值,该方法使用来自以下位置的键和值构建 IConfiguration 之前注册的资源集.

Configuration API constructs key and values using IConfigurationBuilder.Build method that builds IConfiguration with keys and values from the set of sources registered before.

简单来说,将实现以下机制:

If briefly, the following mechanism is implemented:

  • 内部使用特定的提供程序从注册的源中读取.基于文件的源具有适当的 FileConfigurationProvider .对于JSON文件,这是 JsonConfigurationProvider .
  • IConfigurationBuilder.Build 方法为每个注册的源构造提供程序,并调用 provider.Load 方法.
  • Load 从源中读取数据,并填充键值对的内部 Provider.Data 集合(具体来说是字典).
  • 当您需要通过应用中的键获取值时,Configuration API会使用此 Data 集合.
  • Internally the specific provider is used to read from the registered source. File-based sources have the appropriate FileConfigurationProvider. For JSON files this is a JsonConfigurationProvider.
  • IConfigurationBuilder.Build method constructs providers for each of registered sources and calls the provider.Load method.
  • Load reads data from the source and fill an internal Provider.Data collection (dictionary to be more specific) of key-value pairs.
  • This Data collection is then used by Configuration API when you need to get a value by key in your app.

当Configuration API创建 FileConfigurationProvider 实例时,它将使用 FileConfigurationSource.ReloadOnChange 属性的值(默认情况下为false).如果此属性为true,则提供程序订阅文件更改"事件(在ctor中),以在每次文件更改时调用 Load (并因此重新填充 Data 集合)从 github :

When Configuration API creates the FileConfigurationProvider instance, it uses the value of FileConfigurationSource.ReloadOnChange property (false by default). If this property is true, then provider subscribes to "file changed" event (in ctor) to call Load each time when the file is changed (and so repopulate the Data collection. From github:

if (Source.ReloadOnChange && Source.FileProvider != null)
{
    ChangeToken.OnChange(
         () => Source.FileProvider.Watch(Source.Path),
         () => {
                  Thread.Sleep(Source.ReloadDelay);
                  Load(reload: true);
               });
}

这篇关于阅读appsettings.json是否昂贵?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆