在dotnet核心中读取json文件时出错,“达到了对inotify实例数量的配置用户限制(128)”。 [英] Error while reading json file in dotnet core "the configured user limit (128) on the number of inotify instances has been reached"

查看:1323
本文介绍了在dotnet核心中读取json文件时出错,“达到了对inotify实例数量的配置用户限制(128)”。的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I have an console application (in dot net core 1.1) which is scheduled in cron scheduler for every 1 min. Inside the application there is call to configuration file. I'm attaching the code below.

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)。请找到完整的堆栈跟踪。

After running the application for sometime, this error is getting in my log file "the configured user limit (128) on the number of inotify instances has been reached". Please find the full stack trace.


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

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

Please tell me what is wrong with this code.

推荐答案

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

每次访问设置时,您都在创建文件监视程序。第三个参数是 reloadOnChange

You are creating file watchers, every time you access an setting. The 3rd parameter is reloadOnChange.

您必须确保

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

或更干净:

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

最好的方法是将其抽象到接口后面并使用依赖项注入。

Best way is to abstract that 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;
    }
}

然后实例化并注册

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

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

and inject it into your services via constructor

这篇关于在dotnet核心中读取json文件时出错,“达到了对inotify实例数量的配置用户限制(128)”。的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆