一.写作前题
我们用ASP.NET做项目开发的时候,配置Config文件那是经常的事情,VS.NET的Config文件提供了很多节,但是往往提供的这些配置信息还不能够完全满足我们的项目开发需求,而且微软正是考虑到这方面的因素,他允许用户自定义Configuration的相关配置内容。本文就此写了一些实例,希望对大家有所帮助。
二.本文内容
1.实现web.config中的自定义2.对自定义节的使用3.本文总结三.实现Web.Config中自定义节 废话不多说,直接说主题,这里我们要继承ConfigurationElement,ConfigurationElementCollection,ConfigurationSection等相关的类。首先我们在Config文件中的增加了一个节,我们增加了一个自定义节<section name="commonSectionConfiguration" type="CWS.Framework.Client.ClientAddressSection,CWS.Framework.Client"/>,这个节的具体配置如下所示
1 < commonSectionConfiguration > 2 < CleintAddressCollection > 3 < add Name ="CommonPath" ServiceCommonPath ="http://localhost/CWSHost/SVCService/{0}" IsDefault ="true" > add > 4 CleintAddressCollection > 5 commonSectionConfiguration >
Config文件的整体配置如下所示: 1 < configuration > 2 < configSections > 3 < section name ="commonSectionConfiguration" type ="CWS.Framework.Client.ClientAddressSection,CWS.Framework.Client" /> 4 < sectionGroup name ="system.web.extensions" type ="System.Web.Configuration.SystemWebExtensionsSectionGroup, System.Web.Extensions, Version=3.5.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35" > 5 < sectionGroup name ="scripting" type ="System.Web.Configuration.ScriptingSectionGroup, System.Web.Extensions, Version=3.5.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35" > 6 < section name ="scriptResourceHandler" type ="System.Web.Configuration.ScriptingScriptResourceHandlerSection, System.Web.Extensions, Version=3.5.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35" requirePermission ="false" allowDefinition ="MachineToApplication" /> 7 < sectionGroup name ="webServices" type ="System.Web.Configuration.ScriptingWebServicesSectionGroup, System.Web.Extensions, Version=3.5.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35" > 8 < section name ="jsonSerialization" type ="System.Web.Configuration.ScriptingJsonSerializationSection, System.Web.Extensions, Version=3.5.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35" requirePermission ="false" allowDefinition ="Everywhere" /> 9 < section name ="profileService" type ="System.Web.Configuration.ScriptingProfileServiceSection, System.Web.Extensions, Version=3.5.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35" requirePermission ="false" allowDefinition ="MachineToApplication" /> 10 < section name ="authenticationService" type ="System.Web.Configuration.ScriptingAuthenticationServiceSection, System.Web.Extensions, Version=3.5.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35" requirePermission ="false" allowDefinition ="MachineToApplication" /> 11 < section name ="roleService" type ="System.Web.Configuration.ScriptingRoleServiceSection, System.Web.Extensions, Version=3.5.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35" requirePermission ="false" allowDefinition ="MachineToApplication" /> 12 sectionGroup > 13 sectionGroup > 14 sectionGroup > 15 configSections > 16 < commonSectionConfiguration > 17 < CleintAddressCollection > 18 < add Name ="CommonPath" ServiceCommonPath ="http://localhost/CWSHost/SVCService/{0}" IsDefault ="true" > add > 19 CleintAddressCollection > 20 commonSectionConfiguration > 21 configuration >
通过上面的web.config文件,我本文所使用的自定义节的使用,设置完web.config文件后,我们怎么来使用(获得)我们配置的信息呢? 我们可以从上面看到 type="CWS.Framework.Client.ClientAddressSection,CWS.Framework.Client", 这就告诉我们,通过CWS.Framework.Client命名空间下的ClientAddressSection类来实现这个节的操作。 为了实现对这个节的使用,需要写一个类,他继承于ConfigurationElement类,实现对config中自定义节CleintAddressCollection中属性的映射,请看下面代码。 映射web.config自定义类 1 /*********************************************************** 2 * @Copy Right CWS.Framework.Client V1.0 3 * Function: Provid a class to mapping the web config 4 * 5 * Create By:Xiong Wei Date: 22 August 2009 6 * Update By: Date: 7 * 8 * Review History: 9 * Review By/Date Description10 * 11 ***********************************************************/12 using System;13 using System.Collections.Generic;14 using System.Linq;15 using System.Text;16 using System.Configuration;17 18 namespace CWS.Framework.Client19 { 20 /// 21 /// mapping with configuration file22 /// 23 public sealed class ClientAddressElement : ConfigurationElement24 { 25 [ConfigurationProperty("Name")]26 public string Name27 { 28 get { return this["Name"] as string; }29 set { this["Name"] = value; }30 }31 32 [ConfigurationProperty("ServiceCommonPath")]33 public string ServiceCommonPath34 { 35 get { return (string)this["ServiceCommonPath"]; }36 set { this["ServiceCommonPath"] = value; }37 }38 39 [ConfigurationProperty("IsDefault")]40 public bool IsDefault41 { 42 get { return (bool)this["IsDefault"]; }43 set { this["IsDefault"] = value; }44 }45 }46 }47
通过上面的代码我们可以看到,他实现了对config中CleintAddressCollection属性的映射。在本文中我们只有三个属性,如果有更多的属性我们可以以此类推,实现代码与Config文件中属性的映射关系。 上面我们只是写了一个类来实现对config中属性的映射,但是如何使用这个类呢,请看下面的代码。 创建自定义节对象 1 /*********************************************************** 2 * @Copy Right CWS.Framework.Client V1.0 3 * Function: Get user-defined section 4 * 5 * Create By:Xiong Wei Date: 22 August 2009 6 * Update By: Date: 7 * 8 * Review History: 9 * Review By/Date Description10 * 11 ***********************************************************/12 using System;13 using System.Collections.Generic;14 using System.Linq;15 using System.Text;16 using System.Configuration;17 18 namespace CWS.Framework.Client19 { 20 [ConfigurationCollection(typeof(ClientAddressElement))]21 public sealed class ClientAddressElementCollection : ConfigurationElementCollection22 { 23 static ClientAddressElementCollection()24 { 25 26 }27 28 public new ClientAddressElement this[string key]29 { 30 get { return (ClientAddressElement)base.BaseGet(key); }31 set { base.BaseRemove(key); this.BaseAdd(value); }32 }33 34 protected override ConfigurationElement CreateNewElement()35 { 36 return new ClientAddressElement();37 }38 39 protected override object GetElementKey(ConfigurationElement element)40 { 41 return ((ClientAddressElement)element).Name;42 }43 }44 }45
上面我们在重载方法GetElementKey中指定用Name用为唯一属性。比如说在自定义的节中有多个item,那么我们怎么知道我们需要那一个item呢,这时就需要使用Name来进行标识。
< commonSectionConfiguration > < CleintAddressCollection > < add Name ="CommonPath" ServiceCommonPath ="http://localhost/CWSHost/SVCService/{0}" IsDefault ="true" > add > <add Name="CommonPath1" ServiceCommonPath="http://localhost/DUPHost/SVCService/{0}" IsDefault="true">add> CleintAddressCollection > commonSectionConfiguration >
下面这段代码的作用,是取出我们所需要的那个section的内容,因为我们可能有多个section的类容,我们怎么知道我们现在需要使用那个section的内容,比如说下面的代码,我们就是告诉我们需要取出 CleintAddressCollection 节中的信息。 提取节的代码 1 /*********************************************************** 2 * @Copy Right CWS.Framework.Client V1.0 3 * Function: Provid a class to mapping the section in web config 4 * 5 * Create By:Xiong Wei Date: 22 August 2009 6 * Update By: Date: 7 * 8 * Review History: 9 * Review By/Date Description10 * 11 ***********************************************************/12 using System;13 using System.Collections.Generic;14 using System.Linq;15 using System.Text;16 using System.Configuration;17 18 namespace CWS.Framework.Client19 { 20 public sealed class ClientAddressSection : ConfigurationSection21 { 22 private static readonly ConfigurationProperty _propertyItems;23 private static ConfigurationPropertyCollection _properties;24 25 static ClientAddressSection()26 { 27 _propertyItems = new ConfigurationProperty("CleintAddressCollection", typeof(ClientAddressElementCollection), null, ConfigurationPropertyOptions.IsRequired);28 _properties = new ConfigurationPropertyCollection();29 _properties.Add(_propertyItems);30 }31 32 [ConfigurationProperty("CleintAddressCollection")]33 public ClientAddressElementCollection CleintAddressCollection34 { 35 get { return (ClientAddressElementCollection)base[_propertyItems]; }36 set { base[_propertyItems] = value; }37 }38 39 protected override ConfigurationPropertyCollection Properties40 { 41 get42 { 43 return _properties;44 }45 }46 }47 }48
上面的代码已经为我们取出了所需要的属性,下面我们要做的就是取出这些值,这里就涉及到具体的应用,可以根据自己的需求不定进行自定义的设置。
实现对自定义节的引用 1 /*********************************************************** 2 * @Copy Right CWS.Framework.Client V1.0 3 * Function: Usage the user-defined configuration section information 4 * 5 * Create By:Xiong Wei Date: 22 August 2009 6 * Update By: Date: 7 * 8 * Review History: 9 * Review By/Date Description10 * 11 ***********************************************************/12 using System;13 using System.Collections.Generic;14 using System.Linq;15 using System.Text;16 using System.Configuration;17 18 namespace CWS.Framework.Client19 { 20 public static class ClientAddressOpreate21 { 22 public static string GetCommonPath()23 { 24 Configuration config = ConfigurationManager.OpenExeConfiguration(string.Empty);25 ClientAddressSection section = config.GetSection("commonSectionConfiguration") as ClientAddressSection;26 string elementKey = "CommonPath";27 ClientAddressElement proitem = section.CleintAddressCollection[elementKey];28 if (proitem == null)29 { 30 throw new Exception("None object when get the client common address path");31 }32 return proitem.ServiceCommonPath;33 }34 }35 }36
在我们的例子中,我们提供了一个方法来实现上面功能的调用。
提供可被调用方法 1 using System; 2 using System.Collections.Generic; 3 using System.Linq; 4 using System.Text; 5 6 namespace CWS.Framework.Client 7 { 8 public static class UrlHelper 9 { 10 public static string GetWCFUrl(string endpointSVCName)11 { 12 return string.Format(ClientAddressOpreate.GetCommonPath(), endpointSVCName);13 }14 }15 }16
四.引用自定义节 上面第三节内容的代码我们已经实现了地自定节的实现和操作,下面的类是一个aspx.cs文件,他的作用就是调用上面实现的功能,并把结果输出出来。 Code 1 using System; 2 using System.Collections; 3 using System.Configuration; 4 using System.Data; 5 using System.Linq; 6 using System.Web; 7 using System.Web.Security; 8 using System.Web.UI; 9 using System.Web.UI.HtmlControls;10 using System.Web.UI.WebControls;11 using System.Web.UI.WebControls.WebParts;12 using System.Xml.Linq;13 using CWS.Framework.Client;14 15 namespace Demo16 { 17 public partial class _Default : System.Web.UI.Page18 { 19 protected void Page_Load(object sender, EventArgs e)20 { 21 Response.Write(UrlHelper.GetWCFUrl("test"));22 }23 }24 }
输出结果如下:五.总结1.通过此实现为以后开发其它产品实现自定义类提供了参考。2.实现功能,需要理解web.config与实际代码之间的映射关系。3.对System.Configuration命名空间下的ConfigurationElement,ConfigurationElementCollection,ConfigurationSection类的要有所了解。