1

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....

Camilo Terevinto
  • 31,141
  • 6
  • 88
  • 120
David
  • 2,173
  • 3
  • 25
  • 36
  • 2
    Why don't you just read the configuration file into a Class during `Startup` and make it available through DI? Do you need to get new values for every single request? – Camilo Terevinto Dec 08 '17 at 17:27
  • How do I make said class global for all of the methods in the controller? I can make that global like I have been... then I guess the configuration crap won't be passed around... @CamiloTerevinto – David Dec 08 '17 at 17:39

1 Answers1

2

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;
    }
}
Camilo Terevinto
  • 31,141
  • 6
  • 88
  • 120
  • Ok, this is just perfect. I was able to remove the building of the config in my base class (and therefore all the others) and utilize it in the startup class as you mention here. Now... the big question is will this help me with my memory problems? Thanks for the help! – David Dec 08 '17 at 19:08
  • @David It should help with the memory issue since you are only ever using a single instance of the class – Camilo Terevinto Dec 08 '17 at 19:11
  • Ok... just as an update. My memory issues completely disappeared after I implemented this change. HUGE change. Thank you so much. @Camilo – David Feb 06 '18 at 16:28