ASP.Net Core 2配置占用了大量内存。 如何以不同方式获取配置信息?

好的,所以我有一个应用程序可以获得相当多的流量。 我一直在与Microsoft Azure和Coding团队合作解决内存问题。 他们已经看到了GB的日志,以及当我们处于高负荷状态时,发现Microsoft.Extensions.Configuration代码占据了RAM的大部分份额。

在我的API代码中,我有一个“基本控制器”,所有其他控制器都inheritance自。 这允许我分享常用方法等。 在这个基本控制器中,我创建了一个全局变量:

public IConfigurationRoot _configuration { get; } 

我相信这是罪魁祸首……但我不知道如何摆脱它。 这个_configuration变量允许我访问我的appsettings.json环境变量。 我不知道如何以不同的方式访问这些。

例如……在GET调用中,我需要知道是否有缓存。

  bool isCaching = bool.Parse(_configuration["Data:Cache"]); 

我有一个想法是将_configuration设置为BaseController私有,并在其中创建方法以获取我需要的属性(即缓存),以便其他控制器不必传递此_configuration对象。 不确定是否私有会做任何事情,但….

我不确定为什么你需要一遍又一遍地解析相同的值,当你可以在启动期间读取配置文件并重用它时:

 public class MyConfiguration { public bool CachingEnabled { get; set; } // more configuration data } public void ConfigureServices(IServiceCollection services) { // your existing configuration var myConfiguration = new MyConfiguration { CachingEnabled = bool.Parse(Configuration["Data:Cache"]), // other properties } // register the data as a singleton since it won't change services.AddSingleton(myConfiguration); } public class MyController : Controller { private readonly MyConfiguration configuration; public MyController(MyConfiguration config) { configuration = config; } }