zoukankan      html  css  js  c++  java
  • 记一次读取json配置文件,引发的堆栈溢出

    读取配置文件的原代码

    /// <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");
                    });

    程序加载时将配置加入内存中

    从此再无堆栈溢出

  • 相关阅读:
    Section 3.1 Shaping Regions
    3D@OpenSource
    查找资料
    Section 3.1 Shaping Regions Again
    USACO Contact IOI’98 TLE
    事项ON丰宁坝上草原
    四叉树@POJ1610 Quad Trees
    在TabCtrl上放View@MFC
    CUGB的一场周赛
    贴图程序进展
  • 原文地址:https://www.cnblogs.com/jianghaidong/p/14084877.html
Copyright © 2011-2022 走看看