I am now working in a game company and recently working on a project. I need to create a GM management backend and call interfaces provided by other companies to implement background management operations.
Because the interface addresses are fixed, you can configure all interfaces to Web. config by using custom nodes.
Soon, V1.0 was released:
public class RequestConfigSection : ConfigurationSection { [ConfigurationProperty("sources", IsDefaultCollection = true)] [ConfigurationCollection(typeof(RequestConfigSourceCollection), AddItemName = "add")] public RequestConfigSourceCollection ConfigCollection { 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(ConfigurationElement element) { return ((RequestConfigSource)element).Name; } /// <summary> /// 获取所有键 /// </summary> public IEnumerable<string> AllKeys { get { return BaseGetAllKeys().Cast<string>(); } } /// <summary> /// 索引器 /// </summary> /// <param name="name"></param> /// <returns></returns> public new RequestConfigSource this[string name] { get { return (RequestConfigSource)BaseGet(name); } } } public class RequestConfigSource : ConfigurationElement { /// <summary> /// 名称 /// </summary> [ConfigurationProperty("name")] public string Name { get { return (string)this["name"]; } set { this["name"] = value; } } /// <summary> /// 地址 /// </summary> [ConfigurationProperty("url")] public string Url { get { return (string)this["url"]; } set { this["url"] = value; } } /// <summary> /// 访问类型 /// </summary> [ConfigurationProperty("type")] public RequestType RequestType { get { return (RequestType)Enum.Parse(typeof(RequestType), this["type"].ToString(), true); } set { this["type"] = value; } } }
The configuration method in Web. config is as follows:
<apiRequestConfig> <sources> <add name="..." url="..." type="POST" /> <add name="..." url="..." type="POST" /> <add name="..." url="..." type="POST" /> </sources></apiRequestConfig>
At this time, I read the requirement document again and found that the interface addresses of different platforms are different, but the interfaces do the same thing.
Then I started to think that, if I append an object below, the names of the same interface on different platforms cannot be the same.
The ideal configuration method is:
<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>
However, nodes with the sources name can only appear once... Okay, it hurts.
After trying this research for a long time, I did not find a proper solution. I was too lazy to write a new set of code to read XML ,... Start searching for solutions on the Internet
Keywords cannot be found in Chinese... When the wall is turned over, the keyword "one or more configurationelementcollection" is used in English...
Finally, an alternative solution was found in a foreigner's blog. The final configuration is as follows:
<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 # The Code is as follows:
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(ConfigurationElement element) { return ((RequestConfigSource)element).Name; } /// <summary> /// 获取所有键 /// </summary> public IEnumerable<string> AllKeys { get { return BaseGetAllKeys().Cast<string>(); } } /// <summary> /// 索引器 /// </summary> /// <param name="name"></param> /// <returns></returns> public new RequestConfigSource this[string name] { get { return (RequestConfigSource)BaseGet(name); } } } /// <summary> /// 请求的配置信息 /// </summary> public class RequestConfigSource : ConfigurationElement { /// <summary> /// 名称 /// </summary> [ConfigurationProperty("name")] public string Name { get { return (string)this["name"]; } set { this["name"] = value; } } /// <summary> /// 地址 /// </summary> [ConfigurationProperty("url")] public string Url { get { return (string)this["url"]; } set { this["url"] = value; } } /// <summary> /// 访问类型 /// </summary> [ConfigurationProperty("type")] public RequestType RequestType { get { return (RequestType)Enum.Parse(typeof(RequestType), this["type"].ToString(), true); } set { this["type"] = value; } } }
My development environment is. NET Framework 4.0
InitiallyRequestconfigsectionClassConfigcollectionAndRequestconfigtypeClassSourcecollectionNot DefinedConfigurationcollectionFeatures
InsteadRequestconfigtypecollectionAndRequestconfigtypecollectionOverloadedElementnameAttribute, return the child node name.
An undefined node name exception is thrown...
Change FeatureConfigurationcollectionDefine and give the propertyAdditemnameAssign a sub-level node name...
Foreigner blog address: http://tneustaedter.blogspot.com/2011/09/how-to-create-one-or-more-nested.html needs FQ access
Or directly look here, I copy it out: http://www.cnblogs.com/efenghuo/articles/4022836.html
Custom configurationsection to create multiple nested configurationelementcollection nodes