如何禁用默认的aspnet核心配置更改观察程序?

时间:2019-05-29 12:30:54

标签: docker configuration-files asp.net-core-2.1 filesystemwatcher

我有一个在Docker容器中运行的ASPNET核心应用程序。目前,我创建的图像不能启动超过64个容器(initially described here)。

通过交互运行容器,我在启动阶段发现了此错误。

  

未处理的异常:System.IO.IOException:已达到对inotify实例数量的配置的用户限制(128)。

     

在System.IO.FileSystemWatcher.StartRaisingEvents()      在System.IO.FileSystemWatcher.StartRaisingEventsIfNotDisposed()      在System.IO.FileSystemWatcher.set_EnableRaisingEvents(布尔值)      在Microsoft.Extensions.FileProviders.Physical.PhysicalFilesWatcher.TryEnableFileSystemWatcher()      在Microsoft.Extensions.FileProviders.Physical.PhysicalFilesWatcher.CreateFileChangeToken(字符串过滤器)      在Microsoft.Extensions.FileProviders.PhysicalFileProvider.Watch(字符串过滤器)      在Microsoft.Extensions.Configuration.FileConfigurationProvider。<。ctor> b__0_0()

     Microsoft.Extensions.Primitives.ChangeToken.OnChange上的

(Func`1 changeTokenProducer,操作changeTokenConsumer)      在Microsoft.Extensions.Configuration.FileConfigurationProvider..ctor(FileConfigurationSource源)      在Microsoft.Extensions.Configuration.Json.JsonConfigurationSource.Build(IConfigurationBuilder构建器)      在Microsoft.Extensions.Configuration.ConfigurationBuilder.Build()      

     

Microsoft.AspNetCore.Hosting.WebHostBuilder.BuildCommonServices(AggregateException&HostingStartupErrors)      在Microsoft.AspNetCore.Hosting.WebHostBuilder.Build()      在/src/MyProjectName/Program.cs:line 10中的MyProjectName.Program.Main(String [] args)处

Program.cs仅具有默认内容

 1 using Microsoft.AspNetCore;
 2 using Microsoft.AspNetCore.Hosting;
 3
 4 namespace CmcIiiCgiSim
 5 {
 6    public class Program
 7    {
 8        public static void Main(string[] args)
 9        {
10            CreateWebHostBuilder(args).Build().Run();
11        }
12
13        public static IWebHostBuilder CreateWebHostBuilder(string[] args) =>
14            WebHost.CreateDefaultBuilder(args)
15                .UseStartup<Startup>();
16     }
17 }

看来,监视配置更改是ASP或任何ASPNET核心应用(see MS documentation)的默认设置

我的问题是:如何在现有配置文件的设置中禁用此选项? 希望这也将禁用文件系统监视程序并摆脱该错误。

我发现了很多示例,这些示例说明了如何以编程方式完成其他设置文件。但就我而言,配置不会改变,我宁愿拥有一个不限于64个容器的映像。

1 个答案:

答案 0 :(得分:0)

对于默认的构建器,他非常自以为是,并使用已设置的重新加载来加载应用程序设置。无法替换默认构建器中设置的配置。从.NET Core 3.1.7开始,这是CreateDefaultBuilder方法,其标志已更改为停止重新加载。我已经使用此配置运行服务器,并确认未创建FileSystemWatchers。

public static IHostBuilder CreateDefaultBuilder(string[] args)
    {
        HostBuilder hostBuilder = new HostBuilder();
        hostBuilder.UseContentRoot(Directory.GetCurrentDirectory());
        hostBuilder.ConfigureHostConfiguration((Action<IConfigurationBuilder>) (config =>
        {
            config.AddEnvironmentVariables("DOTNET_");
            config.AddCommandLine(args);
        }));
        hostBuilder.ConfigureAppConfiguration((hostingContext, config) =>
            {
                IHostEnvironment hostingEnvironment = hostingContext.HostingEnvironment;
                config.AddJsonFile("appsettings.json", true, false)
                    .AddJsonFile("appsettings." + hostingEnvironment.EnvironmentName + ".json", true, false);
                if (hostingEnvironment.IsDevelopment() &&
                    !string.IsNullOrEmpty(hostingEnvironment.ApplicationName))
                {
                    Assembly assembly = Assembly.Load(new AssemblyName(hostingEnvironment.ApplicationName));
                    if (assembly != null)
                        config.AddUserSecrets(assembly, true);
                }

                config.AddEnvironmentVariables();
                config.AddCommandLine(args);
            })
            .ConfigureLogging((hostingContext, logging) =>
            {
                int num = RuntimeInformation.IsOSPlatform(OSPlatform.Windows) ? 1 : 0;
                if (num != 0)
                    logging.AddFilter<EventLogLoggerProvider>(level => level >= LogLevel.Warning);
                logging.AddConfiguration(hostingContext.Configuration.GetSection("Logging"));
                logging.AddConsole();
                logging.AddDebug();
                logging.AddEventSourceLogger();
                if (num == 0)
                    return;
                logging.AddEventLog();
            })
            .UseDefaultServiceProvider((Action<HostBuilderContext, ServiceProviderOptions>) ((context, options) =>
            {
                bool flag = context.HostingEnvironment.IsDevelopment();
                options.ValidateScopes = flag;
                options.ValidateOnBuild = flag;
            }));
        return hostBuilder;
    }