0

Environment: windows 10, .net core 3.1, console application with selenium/chromedriver. At the time of execution, typical CPU usage is 80%, RAM at 70%.

There have been multiple discussion regarding how to read appsettings.json on SO, for example, this and this both make use of ConfigurationBuilder class.

Learning from these threads, here's my implementation:

class ConfigReader 
    {
        private IConfigurationRoot _configuration;

        public ConfigReader()
        {
            var time1 = DateTime.Now;

            var builder = new ConfigurationBuilder()
                    .SetBasePath(Directory.GetCurrentDirectory())  
                    .AddJsonFile("appsettings.json", optional: false, reloadOnChange: true);

            var time2 = DateTime.Now;
            var ts12 = time2.Subtract(time1);
            if (ts12.TotalMinutes > 1)
                Console.WriteLine($"CF1-2: {ts12.TotalMinutes} minutes");

            _configuration = builder.Build();      // this line could take forever

            var time3 = DateTime.Now;
            var ts23 = time3.Subtract(time2);
            if (ts23.TotalMinutes > 1)
                Console.WriteLine($"CF2-3: {ts23.TotalMinutes} minutes");
        }

        public string GetConfiguration(string sConfigName)
        {
            return _configuration.GetSection(sConfigName).Value;            
        }
    }

My console application will use this class from various places, and sometimes this line takes a very long time to execute:

_configuration = builder.Build();

Here's one of the outputs from the second writeline in the code snippet

CF2-3: 22.449799578333334 minutes

It took more than 22 minutes to execute. The problem doesn't happen every time, most of the runs are normal except a few going crazy.

Any suggestions are appreciated. If you need additional information please let me know. Thank you.

Cal
  • 747
  • 1
  • 13
  • 30
  • Something isn't right. It cannot take forever to build your configuration file. Could your config file be locked in some way? – Ankit Vijay Apr 04 '21 at 20:41
  • @AnkitVijay No other application accessing/modifying the file. Could it be the hard drive going bad? It's an old laptop. – Cal Apr 04 '21 at 20:52
  • Could you try a different machine? – Ankit Vijay Apr 04 '21 at 20:53
  • @AnkitVijay let me try that, thank you. – Cal Apr 04 '21 at 20:57
  • Even with a console application you really shouldn't be building the config manually like that and should preferably be using the generic `Host`, adding to the builder only (don't call Build) and then injecting `IConfiguration` or strongly typed options. – pinkfloydx33 Apr 04 '21 at 21:32

0 Answers0