<output id="qn6qe"></output>

    1. <output id="qn6qe"><tt id="qn6qe"></tt></output>
    2. <strike id="qn6qe"></strike>

      亚洲 日本 欧洲 欧美 视频,日韩中文字幕有码av,一本一道av中文字幕无码,国产线播放免费人成视频播放,人妻少妇偷人无码视频,日夜啪啪一区二区三区,国产尤物精品自在拍视频首页,久热这里只有精品12

          Asp.net core可以監(jiān)視json、xml等配置文件的變化, 自動(dòng)刷新內(nèi)存中的配置內(nèi)容, 但如果想每隔1秒從zookeeper、consul獲取最新的配置信息, 需要自己實(shí)現(xiàn).

         閱讀了 Asp.net core Document的Custom configuration provider, 得知只需要實(shí)現(xiàn)自己的IConfigurationSource和對(duì)應(yīng)ConfigurationProvider即可

         在這個(gè)示例中, 我建立了一個(gè)簡(jiǎn)單的option, 只包含一個(gè)不斷變化的計(jì)數(shù)器變量.

      public class RefreshableOptions
      {
          public int IncreasementCount { get; set; }
      }

          實(shí)現(xiàn)IConfigurationSource和對(duì)應(yīng)ConfigurationProvider, 內(nèi)部有一個(gè)timer模擬從外部獲取了最新的數(shù)據(jù), 這里為簡(jiǎn)單起見, 采用硬編碼的方式指定了option的路徑

      public class AutoRefreshConfigurationSource : IConfigurationSource
      {
          public IConfigurationProvider Build(IConfigurationBuilder builder)
          {
              return new AutoRefreshConfigurationProvider();
          }
      }
      
      public class AutoRefreshConfigurationProvider : ConfigurationProvider
      {
          private int count = 0;
          private bool isChanged;
      
          public AutoRefreshConfigurationProvider() : base()
          {
              Timer timer = new Timer(TimerCallback);
              timer.Change(1000, 3000);
          }
      
          public override void Load()
          {
              var beforeData = Data;
              // 這里采用硬編碼指定option的路徑
              Data = new Dictionary<string, string>() { { "AutoRefreshOptions:IncreasementCount", count.ToString() } };
              isChanged = IsDictionaryChanged(beforeData, Data);
          }
      
          private void TimerCallback(object state)
          {
              count++;
              this.Load();
              if (isChanged)
              {
                  base.OnReload();//通知IConfiguration實(shí)例, 有參數(shù)發(fā)生了改變
                  isChanged = false;
              }
          }
          //判斷兩個(gè)Idictionary是否有不同的幫助方法
          private static bool IsDictionaryChanged(IDictionary<string, string> before, IDictionary<string, string> after)
          {
              if (before == null && after == null)
              {
                  return false;
              }
              if ((before == null) != (after == null))
              {
                  return true;
              }
              if (before.Count != after.Count)
              {
                  return true;
              }
              var ignoreCaseBefore = new Dictionary<string, string>(before, StringComparer.OrdinalIgnoreCase);
              foreach (var afterItemKey in after.Keys)
              {
                  if (!ignoreCaseBefore.TryGetValue(afterItemKey, out var beforeItemValue))
                  {
                      return true;
                  }
                  if (beforeItemValue != after[afterItemKey])
                  {
                      return true;
                  }
                  ignoreCaseBefore.Remove(afterItemKey);
              }
              if (ignoreCaseBefore.Count > 0)
              {
                  return true;
              }
              return false;
          }
      }

      實(shí)現(xiàn)擴(kuò)展方法

      public static class AutoRereshConfigurationExtensions
      {
          public static IConfigurationBuilder AddAutoRereshConfiguration(this IConfigurationBuilder builder)
          {
              return builder.Add(new AutoRefreshConfigurationSource());
          }
      }

       

       

      使用方法

      新建一個(gè)WebApi項(xiàng)目, 在Program.CreateWebHostBuilder中增加黃色部分

      WebHost.CreateDefaultBuilder(args)
          .ConfigureAppConfiguration(config =>
          {
              config.AddAutoRereshConfiguration();
          })
          .UseStartup<Startup>();

      在Startup. ConfigureServices中配置

      services.Configure<RefreshableOptions>(Configuration.GetSection("AutoRefreshOptions"));

      修改ValuesController

      [Route("api/[controller]")]
      [ApiController]
      public class ValuesController : ControllerBase
      {
          private RefreshableOptions refreshableOptions;
          public ValuesController(IOptionsSnapshot<RefreshableOptions> refreshableOptions)
          {
              this.refreshableOptions = refreshableOptions.Value;
          }
      
          [HttpGet]
          public ActionResult<IEnumerable<string>> Get()
          {
              return new string[] { "value1", "value2", refreshableOptions.IncreasementCount.ToString() };
          }
      }

       啟動(dòng)后不停的刷新http://localhost:5000/api/values可以看到返回內(nèi)容的變化

       

      本文代碼

       

      后記: 這個(gè)功能寫得比較早, 后來(lái)在nuget上發(fā)現(xiàn)很多consul configure的擴(kuò)展, 這段代碼就全當(dāng)練習(xí)吧.

      posted on 2019-03-14 09:18  zhouandke  閱讀(1487)  評(píng)論(0)    收藏  舉報(bào)
      主站蜘蛛池模板: 高潮潮喷奶水飞溅视频无码| 乱中年女人伦av三区| 在线天堂最新版资源| 香蕉EEWW99国产精选免费| 日本偷拍自影像视频久久| 在线观看国产成人AV天堂| 成人3D动漫一区二区三区| 亚洲精品国产综合久久一线| 欧洲熟妇色xxxxx欧美| 久久精品国产一区二区蜜芽| 国产av一区二区午夜福利| 亚洲成精品动漫久久精久| 精品国产成人a在线观看| 色悠悠在线观看入口一区| 色欲av蜜桃一区二区三| 亚洲国产成人久久综合同性 | 国产老熟女一区二区三区| 2021亚洲va在线va天堂va国产| 国产精品熟妇视频国产偷人| 国产色无码精品视频免费| 西畴县| 99在线精品视频观看免费| 久久青青草原亚洲AV无码麻豆| 伊人精品成人久久综合97| 蜜臀av一区二区三区不卡| 中文字幕人妻无码一区二区三区| 亚洲av乱码久久亚洲精品| 亚洲国模精品一区二区| 午夜射精日本三级| 国产精品国产三级国av| 2021亚洲va在线va天堂va国产| 久热色精品在线观看视频| 久久精品国产99久久久古代| 高清有码国产一区二区| 亚洲国产综合精品2020| 香蕉亚洲欧洲在线一区| 亚洲午夜久久久影院伊人| 日韩中文字幕一二三视频| 精品福利视频一区二区三区| 国产一区二区三区内射高清| 国产成人亚洲综合app网站|