• <fieldset id="8imwq"><menu id="8imwq"></menu></fieldset>
  • <bdo id="8imwq"><input id="8imwq"></input></bdo>
    最新文章專題視頻專題問答1問答10問答100問答1000問答2000關鍵字專題1關鍵字專題50關鍵字專題500關鍵字專題1500TAG最新視頻文章推薦1 推薦3 推薦5 推薦7 推薦9 推薦11 推薦13 推薦15 推薦17 推薦19 推薦21 推薦23 推薦25 推薦27 推薦29 推薦31 推薦33 推薦35 推薦37視頻文章20視頻文章30視頻文章40視頻文章50視頻文章60 視頻文章70視頻文章80視頻文章90視頻文章100視頻文章120視頻文章140 視頻2關鍵字專題關鍵字專題tag2tag3文章專題文章專題2文章索引1文章索引2文章索引3文章索引4文章索引5123456789101112131415文章專題3
    當前位置: 首頁 - 科技 - 知識百科 - 正文

    .NET Core讀取配置文件方式詳細總結

    來源:懂視網 責編:小采 時間:2020-11-27 22:34:59
    文檔

    .NET Core讀取配置文件方式詳細總結

    .NET Core讀取配置文件方式詳細總結:基于.NET Core的跨平臺開發,配置文件與之前.NET Framework采用xml的config文件不同,目前主要是采用json文件鍵值對配置方式讀取。 參考網上相關資料總結如下: 一. 引入擴展 System.Configuration.ConfigurationManager Nuget
    推薦度:
    導讀.NET Core讀取配置文件方式詳細總結:基于.NET Core的跨平臺開發,配置文件與之前.NET Framework采用xml的config文件不同,目前主要是采用json文件鍵值對配置方式讀取。 參考網上相關資料總結如下: 一. 引入擴展 System.Configuration.ConfigurationManager Nuget

    基于.NET Core的跨平臺開發,配置文件與之前.NET Framework采用xml的config文件不同,目前主要是采用json文件鍵值對配置方式讀取。

    參考網上相關資料總結如下:

    一. 引入擴展 System.Configuration.ConfigurationManager

    Nuget 下載擴展,Install-Package System.Configuration.ConfigurationManager

    使用方式:添加配置文件App.config。讀取方式與原.NET Framework方式一致

    優點:兼容.NET Framework 原有配置方式

    缺點:項目運行過程中若需修改App.config文件,對項目中輸出的內容沒有絲毫影響,Debug發現獲取到的值的確沒有變化,需要重新編譯才生效。

    二. 引入擴展 Microsoft.Extensions.Options.ConfigurationExtensions

    Nuget 下載擴展,

    Install-Package Microsoft.Extensions.Options.ConfigurationExtensions

    Install-Package Microsoft.Extensions.Configuration.FileExtensions

    Install-Package Microsoft.Extensions.Configuration.Json

    使用方式:參考微軟官網

    優點:可以讀取application.json中的配置參數,不再使用XML可以說很好的貼近Core的設計理念

    缺點:運行時修改json文件讀取到的內容不會改變,但是至少重啟項目可以修改,若要運行時候修改json文件監聽實現監聽變化。查看源碼,可以發現 雖然配置信息是通過AddSingleton注入的
    但同時也注入了IOptionsChangeTokenSource ,故只需要在獲取配置信息時將IOptions<> 替換為 IOptionsMonitor<>(通過監聽的Option來獲取信息),并通過 IOptionsMonitor<>.CurrentValue獲取即可實時獲取到最新的配置信息(存在修改監聽)

    另外就是,這個方法采用的是反序列化的原理,也就是必須有一個跟配置文件對應的實體類才可以,這個感覺比較雞肋,放棄。

    三. 自定義擴展方法,這個實現自己寫,原理是監聽文件是否變更,來刷新Configuration 配置實現。

    參考園友一個實現,具體需要是否有效,要花時間實踐一下,原鏈接地址,代碼如下:


    public class ConfigurationManager
     {
     /// <summary>
     /// 配置內容
     /// </summary>
     private static NameValueCollection _configurationCollection = new NameValueCollection();
    
     /// <summary>
     /// 配置監聽響應鏈堆棧
     /// </summary>
     private static Stack<KeyValuePair<string, FileSystemWatcher>> FileListeners = new Stack<KeyValuePair<string, FileSystemWatcher>>();
    
     /// <summary>
     /// 默認路徑
     /// </summary>
     private static string _defaultPath = Directory.GetCurrentDirectory() + "\\appsettings.json";
    
     /// <summary>
     /// 最終配置文件路徑
     /// </summary>
     private static string _configPath = null;
    
     /// <summary>
     /// 配置節點關鍵字
     /// </summary>
     private static string _configSection = "AppSettings";
    
     /// <summary>
     /// 配置外連接的后綴
     /// </summary>
     private static string _configUrlPostfix = "Url";
    
     /// <summary>
     /// 最終修改時間戳
     /// </summary>
     private static long _timeStamp = 0L;
    
     /// <summary>
     /// 配置外鏈關鍵詞,例如:AppSettings.Url
     /// </summary>
     private static string _configUrlSection { get { return _configSection + "." + _configUrlPostfix; } }
    
    
     static ConfigurationManager()
     {
     ConfigFinder(_defaultPath);
     }
    
     /// <summary>
     /// 確定配置文件路徑
     /// </summary>
     private static void ConfigFinder(string Path)
     {
     _configPath = Path;
     JObject config_json = new JObject();
     while (config_json != null)
     {
     config_json = null;
     FileInfo config_info = new FileInfo(_configPath);
     if (!config_info.Exists) break;
    
     FileListeners.Push(CreateListener(config_info));
     config_json = LoadJsonFile(_configPath);
     if (config_json[_configUrlSection] != null)
     _configPath = config_json[_configUrlSection].ToString();
     else break;
     }
    
     if (config_json == null || config_json[_configSection] == null) return;
    
     LoadConfiguration();
     }
    
     /// <summary>
     /// 讀取配置文件內容
     /// </summary>
     private static void LoadConfiguration()
     {
     FileInfo config = new FileInfo(_configPath);
     var configColltion = new NameValueCollection();
     JObject config_object = LoadJsonFile(_configPath);
     if (config_object == null || !(config_object is JObject)) return;
     
     if (config_object[_configSection]!=null)
     {
     foreach (JProperty prop in config_object[_configSection])
     {
     configColltion[prop.Name] = prop.Value.ToString();
     }
     }
     
     _configurationCollection = configColltion;
     }
    
     /// <summary>
     /// 解析Json文件
     /// </summary>
     /// <param name="FilePath">文件路徑</param>
     /// <returns></returns>
     private static JObject LoadJsonFile(string FilePath)
     {
     JObject config_object = null;
     try
     {
     StreamReader sr = new StreamReader(FilePath, Encoding.Default);
     config_object = JObject.Parse(sr.ReadToEnd());
     sr.Close();
     }
     catch { }
     return config_object;
     }
    
     /// <summary>
     /// 添加監聽樹節點
     /// </summary>
     /// <param name="info"></param>
     /// <returns></returns>
     private static KeyValuePair<string, FileSystemWatcher> CreateListener(FileInfo info)
     {
    
     FileSystemWatcher watcher = new FileSystemWatcher();
     watcher.BeginInit();
     watcher.Path = info.DirectoryName;
     watcher.Filter = info.Name;
     watcher.IncludeSubdirectories = false;
     watcher.EnableRaisingEvents = true;
     watcher.NotifyFilter = NotifyFilters.Attributes | NotifyFilters.CreationTime | NotifyFilters.DirectoryName | NotifyFilters.FileName | NotifyFilters.LastAccess | NotifyFilters.LastWrite | NotifyFilters.Size;
     watcher.Changed += new FileSystemEventHandler(ConfigChangeListener);
     watcher.EndInit();
    
     return new KeyValuePair<string, FileSystemWatcher>(info.FullName, watcher);
     
     }
    
     private static void ConfigChangeListener(object sender, FileSystemEventArgs e)
     {
     long time = TimeStamp();
     lock (FileListeners)
     {
     if (time > _timeStamp)
     {
     _timeStamp = time;
     if (e.FullPath != _configPath || e.FullPath == _defaultPath)
     {
     while (FileListeners.Count > 0)
     {
     var listener = FileListeners.Pop();
     listener.Value.Dispose();
     if (listener.Key == e.FullPath) break;
     }
     ConfigFinder(e.FullPath);
     }
     else
     {
     LoadConfiguration();
     }
     }
     }
     }
    
     private static long TimeStamp()
     {
     return (long)((DateTime.UtcNow - new DateTime(1970, 1, 1, 0, 0, 0, DateTimeKind.Utc)).TotalMilliseconds * 100);
     }
    
     private static string c_configSection = null;
     public static string ConfigSection
     {
     get { return _configSection; }
     set { c_configSection = value; }
     }
    
    
     private static string c_configUrlPostfix = null;
     public static string ConfigUrlPostfix
     {
     get { return _configUrlPostfix; }
     set { c_configUrlPostfix = value; }
     }
    
     private static string c_defaultPath = null;
     public static string DefaultPath
     {
     get { return _defaultPath; }
     set { c_defaultPath = value; }
     }
    
     public static NameValueCollection AppSettings
     {
     get { return _configurationCollection; }
     }
    
     /// <summary>
     /// 手動刷新配置,修改配置后,請手動調用此方法,以便更新配置參數
     /// </summary>
     public static void RefreshConfiguration()
     {
     lock (FileListeners)
     {
     //修改配置
     if (c_configSection != null) { _configSection = c_configSection; c_configSection = null; }
     if (c_configUrlPostfix != null) { _configUrlPostfix = c_configUrlPostfix; c_configUrlPostfix = null; }
     if (c_defaultPath != null) { _defaultPath = c_defaultPath; c_defaultPath = null; }
     //釋放掉全部監聽響應鏈
     while (FileListeners.Count > 0)
     FileListeners.Pop().Value.Dispose();
     ConfigFinder(_defaultPath);
     }
     }
    
     }

    聲明:本網頁內容旨在傳播知識,若有侵權等問題請及時與本網聯系,我們將在第一時間刪除處理。TEL:177 7030 7066 E-MAIL:11247931@qq.com

    文檔

    .NET Core讀取配置文件方式詳細總結

    .NET Core讀取配置文件方式詳細總結:基于.NET Core的跨平臺開發,配置文件與之前.NET Framework采用xml的config文件不同,目前主要是采用json文件鍵值對配置方式讀取。 參考網上相關資料總結如下: 一. 引入擴展 System.Configuration.ConfigurationManager Nuget
    推薦度:
    標簽: 方法 net 總結
    • 熱門焦點

    最新推薦

    猜你喜歡

    熱門推薦

    專題
    Top 主站蜘蛛池模板: 中文字幕无码久久精品青草| 国产精品久久久久久福利69堂| 国产精品igao视频网| 国产精品男男视频一区二区三区| 国产精品无码专区| 夜夜精品无码一区二区三区 | 国产精品理论片在线观看| 国产精品美女久久久久久2018| 无码人妻精品一区二区三区99不卡| 2021国产精品视频网站| 99国产欧美精品久久久蜜芽| 亚洲Av无码精品色午夜| 亚洲欧洲久久久精品| 日韩一区二区精品观看| 久久乐国产精品亚洲综合| 国产精品视频一区二区三区不卡| 国产精品亚洲午夜一区二区三区| 精品深夜AV无码一区二区| 无码精品人妻一区二区三区免费看| 欧美国产成人久久精品| 久久精品国产国产精品四凭| 久久99亚洲综合精品首页| 国产亚洲曝欧美不卡精品| 国产精品午夜久久| 精品99久久aaa一级毛片| 国语自产精品视频| 久久www免费人成精品香蕉| 精品无码国产自产拍在线观看蜜| 国产亚洲精品国看不卡| 精品国精品国产自在久国产应用男 | 在线观看日韩精品| 日韩精品久久久久久| 久热这里只精品99re8久| 欧美极品欧美精品欧美视频| 日本欧美韩国日本精品| 99精品影院| 国产网红主播无码精品| 精品久久久无码中文字幕| 久久99精品久久久久久不卡| 亚洲国产精品成人午夜在线观看 | 精品精品国产国产|