zoukankan      html  css  js  c++  java
  • .NET程序修改 ConfigurationManager 后,不需要重启IIS也可刷新Web.config配置文件

     

    背景:某些通用的信息需要放置在程序的配置文件Web.config中。当环境变化时,修改了其中的内容,以往都是重启IIS或重启站点或线程池才会生效。现在能不能不重启就实现程序获取到配置文件修改后的内容呢?答案是肯定的。

    实现:在程序读取配置文件时,先刷新某个节点内容。然后读取出来的就是最新修改后的内容了。

    方法一:
    ConfigurationManager.RefreshSection("appSettings");        //刷新 appSettings 节点 (立即生效)
    ConfigurationManager.RefreshSection("connectionString");   //刷新 connectionString 节点 (无法生效 —— 可能是 微软处理时,因为 LocalSqlServer 这个默认配置 而导致的疏忽)

     

     方法二:

    FieldInfo fieldInfo = typeof(ConfigurationManager).GetField("s_initState", BindingFlags.NonPublic | BindingFlags.Static);
    if (fieldInfo != null) fieldInfo.SetValue(null, 0); //将配置文件设置为:未分析状态, 配置文件将会在下次读取时重新分析.
    //立即生效,而且效果明显——就喜欢这种暴力做法。
  • 相关阅读:
    博客园代码
    前端
    1338. Reduce Array Size to The Half
    1220. Count Vowels Permutation
    363. Max Sum of Rectangle No Larger Than K
    366. Find Leaves of Binary Tree
    443. String Compression
    8 · Rotate String
    886. Possible Bipartition
    LT 183 wood cut
  • 原文地址:https://www.cnblogs.com/johsan/p/10833101.html
Copyright © 2011-2022 走看看