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


    https://stackoverflow.com/questions/45875981/error-while-reading-json-file-in-dotnet-core-the-configured-user-limit-128-on

    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

  • 相关阅读:
    简单工厂设计模式
    MVC备忘
    在MVC后台代码中想实现删除时弹出"确认删除"效果
    集合
    嵌套
    整理 补课内容
    百鸡百钱
    ////输入一个100以内的数,判断是不是正整数;
    课后题 5 6
    课后题3,4
  • 原文地址:https://www.cnblogs.com/hanfan/p/10244034.html
Copyright © 2020-2023  润新知