Net Core全局配置讀取管理方法ConfigurationManager
最近在學習.Net Core的過程中,發(fā)現(xiàn).Net Framework中常用的ConfigurationManager在Core中竟然被干掉了。
也能理解。Core中使用的配置文件全是Json,不像Framework使用的XML,暫時不支持也是能理解的,但是畢竟全局配置文件這種東西還挺重要的,閱讀了一些文章后目前有3個解決方案。
一、引入擴展System.Configuration.ConfigurationManager
這個擴展庫可以直接在Nuget中獲取。
使用方法和說明見.NET Core 2.0遷移技巧之web.config配置文件
讀取的文件類型和方法都跟.Net Framework中一致,而且僅需引入包就可以,瞬間很興奮有木有!
但是!在使用過過程中發(fā)現(xiàn)這個擴展有問題。項目運行過程中需修改我的app.config文件,對我項目中輸出的內(nèi)容沒有絲毫影響,Debug發(fā)現(xiàn)獲取到的值的確沒有變化。重啟項目都沒有用。只有把項目重新編譯才好使。
不知道是不是因為我的打開方式不對,但是最終放棄這個方法。
二、引入擴展Microsoft.Extensions.Options.ConfigurationExtensions
這個擴展庫也可以直接在Nuget中獲取。
使用方法和說明見 ASP.NET Core實現(xiàn)類庫項目讀取配置文件
這個可以讀取application.json中的配置參數(shù),不再使用XML可以說很好的貼近Core的設計理念。
可惜,這個也有點美中不足的地方。首先跟上面的那個一樣,運行時修改json文件讀取到的內(nèi)容不會改變,但是至少重啟項目可以修改,這個讓我欣慰很多。另外就是,這個方法采用的是反序列化的原理,也就是必須有一個跟配置文件對應的實體類才可以,這個感覺比較雞肋,放棄。
三、自定義擴展方法
這個是我這次說的重點,要是前面兩個方法能滿足讀者你的需求,那么就沒有必要看下去。
廢話少說,先上代碼:
public class ConfigurationManager
{
/// <summary>
/// 配置內(nèi)容
/// </summary>
private static NameValueCollection _configurationCollection = new NameValueCollection();
/// <summary>
/// 配置監(jiān)聽響應鏈堆棧
/// </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>
/// 配置節(jié)點關鍵字
/// </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>
/// 讀取配置文件內(nèi)容
/// </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>
/// 添加監(jiān)聽樹節(jié)點
/// </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>
/// 手動刷新配置,修改配置后,請手動調(diào)用此方法,以便更新配置參數(shù)
/// </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; }
//釋放掉全部監(jiān)聽響應鏈
while (FileListeners.Count > 0)
FileListeners.Pop().Value.Dispose();
ConfigFinder(_defaultPath);
}
}
}
最開始設計的是采用緩存,每次調(diào)用比對文件的修改時間,大小等特征,出現(xiàn)變化從新載入配置。后來發(fā)現(xiàn)圖樣圖森破!
C#提供了專門監(jiān)聽文件系統(tǒng)的方法。所以從新設計了監(jiān)聽響應鏈堆棧來實現(xiàn)。
使用說明:
1、配置節(jié)點:
可以直接寫在項目默認的配置文件appsettings.json中 格式如下
{
"AppSettings": {
"Title": "Test",
"Version": "1.2.1",
"AccessToken": "123456@abc.com"
}
}
保證配置節(jié)點AppSettings存在,剩下的就是以Key-Value的形式來寫屬性,就可以。
2、外部配置文件
像.Net Framework中一樣,可以通過外部配置文件來實現(xiàn)。格式如下
{
"AppSettings.Url": "D:\\test\\app1.json"
}
采用格式是“配置節(jié)點名.外鏈后綴”的形式??梢栽O計多級外部配置文件,只要發(fā)現(xiàn)有外部配置節(jié)點就會向下尋找,并監(jiān)聽鏈上的所有節(jié)點文件的變化。
但是需要注意的是:一旦存在外部配置節(jié)點,此文件中的配置節(jié)點和參數(shù)將不再參與解析
3、可配置初始化參數(shù)
包括默認文件路徑在內(nèi)的多個參數(shù)均可以修改,詳情見代碼。
修改后需要手動調(diào)用RefreshConfiguration方法,以使配置內(nèi)容生效,有點像事務處理。建議在項目的Startup方法中修改配置方法。
4、使用
跟.Net Framework中一樣,直接調(diào)用ConfigurationManager.Appsettings["Title"]就可以了。
以上就是本文的全部內(nèi)容,希望對大家的學習有所幫助,也希望大家多多支持腳本之家。
- ASP.NET?Core中的Configuration配置二
- ASP.NET?Core中的Configuration配置一
- 淺析.netcore中的Configuration具體使用
- ASP.NET Core Web API 教程Project Configuration
- .Net Core配置Configuration具體實現(xiàn)
- 如何在ASP.NET Core 的任意類中注入Configuration
- .Net Core3.0 配置Configuration的實現(xiàn)
- .NET Core 3.0之創(chuàng)建基于Consul的Configuration擴展組件
- 詳解ASP.NET Core實現(xiàn)強類型Configuration讀取配置數(shù)據(jù)
相關文章
asp.net使用jquery實現(xiàn)搜索框默認提示功能
這篇文章主要介紹了asp.net使用jquery實現(xiàn)搜索框默認提示功能,大家參考使用吧2014-01-01
C# javaScript函數(shù)的相互調(diào)用
如何在JavaScript訪問C#函數(shù),如何在C#中訪問JavaScript的已有變量等實現(xiàn)方法2008-12-12
ASP.NET中在一般處理程序中使用session的簡單介紹
這篇文章介紹了ASP.NET中在一般處理程序中使用session,有需要的朋友可以參考一下2013-10-10
ASP.NET技巧:做個DataList可分頁的數(shù)據(jù)源
ASP.NET技巧:做個DataList可分頁的數(shù)據(jù)源...2006-09-09
asp.net 模擬提交有文件上傳的表單(通過http模擬上傳文件)
通過HTTP模擬GET或POST請求,提交數(shù)據(jù)到服務端獲取響應,比較常見些;但如上傳文件到服務端,使用html form當然簡單了,而因環(huán)境所限有時需要使用模擬方法去提交有附件(文件上傳)的表單。2010-02-02

