读取配置文件的原代码
/// <summary> /// 获取配置 /// </summary> /// <param name="JsonFileName">json文件路径</param> /// <returns></returns> public static IConfiguration GetConfiguration(string JsonFilePath) { //ReloadOnChange = true 当appsettings.json被修改时重新加载 IConfiguration Configuration = new ConfigurationBuilder() .Add(new JsonConfigurationSource { Path = JsonFilePath, ReloadOnChange = true }) .Build(); return Configuration; }
每次需要获取配置信息时调用该方法,读取对应的配置json放到Configuration中
问题的原因:频繁读取时就会一直消耗堆栈内存,运行时间长了之后就会堆栈溢出
后来改成
public static IHostBuilder CreateHostBuilder(string[] args) => Host.CreateDefaultBuilder(args) .ConfigureAppConfiguration((hostingContext, configBuilder) => { configBuilder .AddJsonFile("Config/Common.json"); }) .UseServiceProviderFactory(new AutofacServiceProviderFactory()) .ConfigureWebHostDefaults(webBuilder => { webBuilder.UseStartup<Startup>().UseUrls("http://*:3011"); });
程序加载时将配置加入内存中
从此再无堆栈溢出