[英]Is reading appsettings.json expensive?
Currently I'm developing a .net core 2 application that should read some keys from appsettings.json on each request based on domain.目前我正在开发一个 .net core 2 应用程序,它应该在基于域的每个请求上从 appsettings.json 读取一些键。 (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.
(我不得不提一下,我有一个包含 3 个共享相同 appsettings.json 的项目的单一解决方案)我担心每次请求时 appsettings.json 文件的读取时间。 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 constructs key and values using IConfigurationBuilder.Build
method that builds IConfiguration
with keys and values from the set of sources registered before.配置 API 使用
IConfigurationBuilder.Build
方法构造键和值,该方法使用之前注册的一组源中的键和值构建IConfiguration
。
If briefly, the following mechanism is implemented :如果简单地说,以下机制实现:
FileConfigurationProvider
.FileConfigurationProvider
。 For JSON files this is a JsonConfigurationProvider
.JsonConfigurationProvider
。IConfigurationBuilder.Build
method constructs providers for each of registered sources and calls the provider.Load
method. IConfigurationBuilder.Build
方法为每个注册的源构造提供程序并调用provider.Load
方法。Load
reads data from the source and fill an internal Provider.Data
collection (dictionary to be more specific) of key-value pairs. Load
从源读取数据并填充键值对的内部Provider.Data
集合(更具体地说是字典)。Data
collection is then used by Configuration API when you need to get a value by key in your app.Data
集合。 When Configuration API creates the FileConfigurationProvider
instance, it uses the value of FileConfigurationSource.ReloadOnChange
property (false by default).当 Configuration API 创建
FileConfigurationProvider
实例时,它使用FileConfigurationSource.ReloadOnChange
属性的值(默认为 false)。 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 :如果此属性为 true,则提供程序订阅“文件更改”事件(在 ctor 中)以在每次更改文件时调用
Load
(并因此重新填充Data
集合。来自github :
if (Source.ReloadOnChange && Source.FileProvider != null)
{
ChangeToken.OnChange(
() => Source.FileProvider.Watch(Source.Path),
() => {
Thread.Sleep(Source.ReloadDelay);
Load(reload: true);
});
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.