ASP.Net Core 2 configuration taking up a lot of memory. How do I get config information differently?

前端 未结 1 549
情歌与酒
情歌与酒 2020-12-22 04:54

Ok, so I have an app that gets a pretty good amount of traffic. I have been working with the Microsoft Azure and Coding teams to resolve a problem with memory. They have see

相关标签:
1条回答
  • I am not sure why you need to be parsing the same values over and over again, when you could just read the configuration file during Startup and reuse it:

    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;
        }
    }
    
    0 讨论(0)
提交回复
热议问题