本人現在在一家游戲公司,最近在做一個項目,需要做一個GM的管理后臺,需要調用其他公司提供的接口,來實現后臺管理的操作
由于接口地址都是固定的,所以想到使用自定義節點,來將接口都配置到web.config中。
很快,v1.0版本出爐:
public class RequestConfigSection : ConfigurationSection { [Configuration在web.config中的配置方式為:
<apiRequestConfig> <sources> <add name="..." url="..." type="POST" /> <add name="..." url="..." type="POST" /> <add name="..." url="..." type="POST" /> </sources></apiRequestConfig>
這時候又看了一遍需求文檔,發現有說明不同平臺的接口地址是不一樣的,但接口做的事情是一樣的。
然后就開始想,如果接著在下邊追加,則不同平臺的同一接口的名稱是不能相同的。
所以想到的理想的配置方式為:
<apiRequestConfig> <sources platform="android"> <add name="..." url="..." type="POST" /> <add name="..." url="..." type="POST" /> <add name="..." url="..." type="POST" /> </sources> <sources platform="ios"> <add name="..." url="..." type="POST" /> <add name="..." url="..." type="POST" /> <add name="..." url="..." type="POST" /> </sources></apiRequestConfig>
但是sources 名稱的節點只能出現一次…好吧,蛋疼了。
研究嘗試了一上午也沒有找到合適的解決方式,又懶得再重新寫一套代碼來讀取xml,…開始在網上搜解決方案
用中文做關鍵字找不著…翻了墻,用英文來當關鍵字 one or more ConfigurationElementCollection…
最終在一老外的博客里找到了一個替代的解決方案,最終的配置為:
<apiRequestConfig> <requestConfigs> <request platform="android"> <sources> <add name="..." url="..." type="POST" /> <add name="..." url="..." type="POST" /> <add name="..." url="..." type="POST" /> </sources> </request> <request platform="ios"> <sources> <add name="..." url="..." type="POST" /> <add name="..." url="..." type="POST" /> <add name="..." url="..." type="POST" /> </sources> </request> </requestConfigs> </apiRequestConfig>
C#代碼如下:
public class RequestConfigSection : ConfigurationSection { [ConfigurationProperty("requestConfigs", IsDefaultCollection = true)] [ConfigurationCollection(typeof(RequestConfigTypeCollection), AddItemName = "request")] public RequestConfigTypeCollection ConfigCollection { get { return (RequestConfigTypeCollection)this["requestConfigs"]; } set { this["requestConfigs"] = value; } } /// <summary> /// 根據平臺和名稱獲取請求配置信息 /// </summary> /// <param name="name"></param> /// <param name="platform"></param> /// <returns></returns> public RequestConfigSource GetRequestConfigSource(string platform, string name) { return ConfigCollection[platform].SourceCollection[name]; } } public class RequestConfigTypeCollection : ConfigurationElementCollection { /// <summary> /// 創建新元素 /// </summary> /// <returns></returns> protected override ConfigurationElement CreateNewElement() { return new RequestConfigType(); } /// <summary> /// 獲取元素的鍵 /// </summary> /// <param name="element"></param> /// <returns></returns> protected override object GetElementKey(ConfigurationElement element) { return ((RequestConfigType)element).Platform; } /// <summary> /// 獲取所有鍵 /// </summary> public IEnumerable<string> AllKeys { get { return BaseGetAllKeys().Cast<string>(); } } /// <summary> /// 索引器 /// </summary> /// <param name="name"></param> /// <returns></returns> public new RequestConfigType this[string platform] { get { return (RequestConfigType)BaseGet(platform); } } } public class RequestConfigType : ConfigurationElement { /// <summary> /// 獲取全部請求配置信息 /// </summary> /// <returns></returns> public RequestConfigSource[] GetAllRequestSource() { var keys = this.SourceCollection.AllKeys; return keys.Select(name => this.SourceCollection[name]).ToArray(); } /// <summary> /// 平臺標識 /// </summary> [ConfigurationProperty("platform")] public string Platform { get { return (string)this["platform"]; } set { this["platform"] = value; } } [ConfigurationProperty("sources", IsDefaultCollection = true)] [ConfigurationCollection(typeof(RequestConfigSourceCollection), AddItemName = "add")] public RequestConfigSourceCollection SourceCollection { get { return (RequestConfigSourceCollection)this["sources"]; } set { this["sources"] = value; } } } public class RequestConfigSourceCollection : ConfigurationElementCollection { /// <summary> /// 創建新元素 /// </summary> /// <returns></returns> protected override ConfigurationElement CreateNewElement() { return new RequestConfigSource(); } /// <summary> /// 獲取元素的鍵 /// </summary> /// <param name="element"></param> /// <returns></returns> protected override object GetElementKey(C
新聞熱點
疑難解答