c# - 在 dotnet 核心 "the configured user limit (128) on the number of inotify instances has been reached"中读取 json 文件时出错

标签 c# asp.net-core .net-core .net-standard

我有一个控制台应用程序(在 dot net core 1.1 中)每 1 分钟在 cron 调度程序中安排一次。在应用程序内部调用配置文件。我附上下面的代码。

public static T GetAppConfig<T>(string key, T defaultValue = default(T))
{

    T value = default(T);
    logger.Debug($"Reading App Config {key}");
    try
    {
        var environmentName = Environment.GetEnvironmentVariable("ASPNETCORE_ENVIRONMENT");
        var builder = new ConfigurationBuilder()
            .AddJsonFile($"appsettings.json", true, true)
            .AddJsonFile($"appsettings.{environmentName}.json", true, true)
            .AddEnvironmentVariables();
        var configuration = builder.Build();
        T setting = (T)Convert.ChangeType(configuration[key], typeof(T));
        value = setting;
        if (setting == null)
            value = defaultValue;
    }
    catch (Exception ex)
    {
        logger.Warn(ex, $"An exception occured reading app key {key} default value {defaultValue} applied.");
        value = defaultValue;
    }
    return value;
}

应用程序运行一段时间后,我的日志文件中出现此错误“已达到 inotify 实例数量的配置用户限制 (128)”。请找到完整的堆栈跟踪。

An exception occured reading app key DeviceId default value  applied.
System.IO.IOException: The configured user limit (128) on the number of inotify instances has been reached.
   at System.IO.FileSystemWatcher.StartRaisingEvents()
   at System.IO.FileSystemWatcher.StartRaisingEventsIfNotDisposed()
   at Microsoft.Extensions.FileProviders.Physical.PhysicalFilesWatcher.CreateFileChangeToken(String filter)
   at Microsoft.Extensions.Primitives.ChangeToken.OnChange(Func`1 changeTokenProducer, Action changeTokenConsumer)
   at Microsoft.Extensions.Configuration.Json.JsonConfigurationSource.Build(IConfigurationBuilder builder)
   at Microsoft.Extensions.Configuration.ConfigurationBuilder.Build()
   at app.Shared.Utilities.GetAppConfig[T](String key, T defaultValue) in /var/app/source/app/app.Shared/Utilities.cs:line 33
   at System.IO.FileSystemWatcher.StartRaisingEvents()
   at System.IO.FileSystemWatcher.StartRaisingEventsIfNotDisposed()
   at Microsoft.Extensions.FileProviders.Physical.PhysicalFilesWatcher.CreateFileChangeToken(String filter)
   at Microsoft.Extensions.Primitives.ChangeToken.OnChange(Func`1 changeTokenProducer, Action changeTokenConsumer)
   at Microsoft.Extensions.Configuration.Json.JsonConfigurationSource.Build(IConfigurationBuilder builder)
   at Microsoft.Extensions.Configuration.ConfigurationBuilder.Build()
   at app.Shared.Utilities.GetAppConfig[T](String key, T defaultValue) in /var/app/source/app/app.Shared/Utilities.cs:line 33

请告诉我这段代码有什么问题。

最佳答案

var builder = new ConfigurationBuilder()
        .AddJsonFile($"appsettings.json", true, true);

每次访问设置时,您都在创建文件观察器。第三个参数是reloadOnChange

你必须确定,

var configuration = builder.Build()

仅在您的应用程序中被调用一次并将其存储在您可以访问它的地方(最好是避免它的静态字段)。

或者只是禁用文件观察器。

  var builder = new ConfigurationBuilder()
        .AddJsonFile($"appsettings.json", true, false);

或清洁工:

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

最好的方法是在接口(interface)后面抽象它并使用依赖注入(inject)。

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;
    }
}

然后实例化并注册

services.AddSingleton<IConfigurationManager>(new ConfigurationManager(this.Configuration));

并通过构造函数将其注入(inject)到您的服务中

关于c# - 在 dotnet 核心 "the configured user limit (128) on the number of inotify instances has been reached"中读取 json 文件时出错,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45875981/

相关文章:

c# - 第二个线程在第一个线程释放之前进入锁

c# - 如何以干净的方式实现原始类型的扩展属性类?

c# - MVC 6引用问题

c# - 为什么有些结构继承接口(interface),但不实现其所有成员?

asp.net-core - .net core 添加 Web 引用

c# - 使用 "Control.CheckForIllegalCrossThreadCalls = false"是个好主意吗

azure - 项目在 VS 中构建,但不在 Azure Pipeline 中构建,有助于理解错误

c# - ReCaptcha V3 - 验证失败怎么办?

.net - 获取多个多边形的总边界框(使用 C# NetCore NetTopologySuite)

c# - 如何使用 ASP.NET C# 立即执行下载?