Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
599 views
in Technique[技术] by (71.8m points)

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

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 seen the GB's of logs and how found that the Microsoft.Extensions.Configuration code is taking up a lion's share of the RAM when we are under heavy load.

In my API code I have a "base controller" that all of the other controllers inherit from. This allows me to share common methods and the like. In this base controller I have created a global variable:

   public IConfigurationRoot _configuration { get; }

This is, I believe, the culprit... but I am not sure how to get rid of it. This _configuration variable allows me to get access to my appsettings.json environment variables. I am not sure how to get access to these in a different way.

For instance... in a GET call I need to know if we have caching on or not.

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

One thought I had is to make the _configuration private to the BaseController and make methods inside of there to get the properties I need (i.e. caching) so that the other controllers don't have to pass around this _configuration object. Not sure if make it private will do anything though....

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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;
    }
}

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...