欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

.net core 发布linux报错“The configured user limit (128) on the number of inotify instances has been reached”

程序员文章站 2022-04-05 10:08:18
https://*.com/questions/45875981/error while reading json file in dotnet core the configured user limit 128 on You are creating file watch ......

var builder = new configurationbuilder()
        .addjsonfile($"appsettings.json", true, true);

you are creating file watchers, every time you access an setting. the 3rd parameter is reloadonchange.
you have to make sure,

var configuration = builder.build()

is only called once in your application and store it in a place where you can access it (preferably avoid static fields for it).

or just disable the file watcher.

  var builder = new configurationbuilder()
        .addjsonfile($"appsettings.json", true, false);

or cleaner:

var builder = new configurationbuilder()
        .addjsonfile($"appsettings.json", optional: true, reloadonchange: false);

best way is to abstract hat behind an interface and use dependency injection.

public interface iconfigurationmanager
{
    t getappconfig<t>(string key, t defaultvalue = default(t));
}

public class configurationmanager : iconfigurationmanager
{
    private readonly iconfigurationroot config;

    public configurationmanager(iconfigurationroot config)
        => this.config ?? throw new argumentnullexception(nameof(config));

    public t getappconfig<t>(string key, t defaultvalue = default(t))
    {
        t setting = (t)convert.changetype(configuration[key], typeof(t));
        value = setting;
        if (setting == null)
            value = defaultvalue;
    }
}

then instantiate and register it

services.addsingleton<iconfigurationmanager>(new configurationmanager(this.configuration));

and inject it into your services via constructor