Configuration Builder leaves configuration file open RRS feed

  • Question

  • User227221424 posted


    I know it is bad practice to run code like the following regularly in an application, however it does seem to leave files open.

    Therefore if it is run in a controller method there will be a "Too many open file handles ' error after several hundred requests/.

    Is it to be expected that the configuration file will not be closed within the scope of the method that uses it ??


                var builder = new ConfigurationBuilder()
                    .AddJsonFile($"appsettings.json", optional: false, reloadOnChange: true);
                var conf = builder.Build();
    Friday, June 21, 2019 4:29 PM

All replies

  • User753101303 posted


    I suspect this is caused by reloadOnChange: true (which uses likely a file watcher, and if I follow the doc I do see that the returned object is disposable).

    Try maybe false to see if it changes something (even if this is not what you want ???) or try to dispose this object ???

    Actually, I'm really not sure to get what is the purpose of doing that from your controller ? You are trying precisely to reload explicitly your settings this way???

    Friday, June 21, 2019 4:58 PM
  • User227221424 posted

    Thanks for the quick response. The code was bad, but I was exploring the side effect of the fact that it was leaving the file open. I will try changing to false.

    Friday, June 21, 2019 5:13 PM
  • User753101303 posted

    Still unclear. if you have this in your controller to test what happens when calling that once at startup don't base your decision on that :
    - if you want to be able to edit the settings file and have this be taken into account leave that (and more likely the file is not left open but you consume a file system handle used by a mechanism used to handle changes in this directory)
    - if not needed you can disable that and you'll save just more or less a single handle

    Saturday, June 22, 2019 11:51 AM