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.