类库探源——System.Configuration 配置信息处理
按照MSDN描述 System.Configuration 命名空间 包含处理配置信息的类型
本篇文章主要两方面的内容
1. 如何使用ConfigurationManager 读取AppSetting和ConnectionStrings
2. 如何使用自定义 Section,我这里的自定义Section格式为
<SectionName> <services> 服务1的描述信息,供IoC容器使用 服务2的描述信息,供IoC容器使用 。。。 </services> </SectionName>
其实如果读者使用一些比较出名的IoC 框架(Unity)都会有配置管理,根本不用自定义Section,但是技多不压身,多了解点东西总是没错的。
一、ConfigurationManager 类
提供对客户端应用程序配置文件的访问。无法继承此类
命名空间: System.Configuration
程序集: System.Configuration.dll
继承关系:
原型定义:public static class ConfigurationManager
由上面的定义可以看出ConfigurationManager是一个静态类
静态属性:
1. ConfigurationManager.AppSettings 获取当前应用程序默认配置的 AppSettingsSection 数据
原型定义:public static NameValueCollection AppSettings { get; }
从上面的定义可以看出 AppSetting 是一个 键值对
2. ConfigurationManager.ConnectionStrings 获取当前应用程序默认配置的 ConnectionStringsSection 数据
例子:
app.config
1 <?xml version="1.0"?> 2 <configuration> 3 <startup> 4 <supportedRuntime version="v2.0.50727"/> 5 </startup> 6 7 <connectionStrings> 8 <add name="KKSEntities" connectionString="metadata=res://*/DbModel.csdl|res://*/DbModel.ssdl|res://*/DbModel.msl;provider=System.Data.SqlClient;provider connection string="data source=XXX.XXX.XXX.XXX;initial catalog=KKS;user id=XXXX;password=KKKKK;multipleactiveresultsets=True;App=EntityFramework"" providerName="System.Data.EntityClient" /> 9 </connectionStrings> 10 11 <appSettings> 12 <add key="Aphasia" value="www.cnblogs.com/Aphasia"/> 13 </appSettings> 14 </configuration>
Program.cs
1 using System; 2 using System.Configuration; 3 4 namespace ConsoleApplication1 5 { 6 class Program 7 { 8 static void Main(string[] args) 9 { 10 string web = ConfigurationManager.AppSettings["Aphasia"]; 11 Console.WriteLine(web); 12 13 string cnstr = ConfigurationManager.ConnectionStrings["KKSEntities"].ConnectionString; 14 Console.WriteLine(cnstr); 15 16 } 17 } 18 }
效果:
二、 自定义Section
app.config
1 <?xml version="1.0"?> 2 <configuration> 3 <configSections> 4 <section name="ServicesSection" type="CustomDemo.ServicesSection,CustomDemo"/> 5 </configSections> 6 7 <ServicesSection> 8 <services> 9 <add ServiceName="XXX服务1" Impl="XXX.Impl.XXXService1" Inter="XXX.Inter.XXXInter1" /> 10 <add ServiceName="XXX服务2" Impl="XXX.Impl.XXXService2" Inter="XXX.Inter.XXXInter2" /> 11 <add ServiceName="XXX服务3" Impl="XXX.Impl.XXXService3" Inter="XXX.Inter.XXXInter3" /> 12 </services> 13 </ServicesSection> 14 15 <startup> 16 <supportedRuntime version="v2.0.50727"/> 17 </startup> 18 </configuration>
Program.cs
1 using System; 2 using System.Configuration; 3 4 namespace CustomDemo 5 { 6 class Program 7 { 8 static void Main(string[] args) 9 { 10 ServicesSection myServices = ConfigurationManager.GetSection("ServicesSection") as ServicesSection; 11 foreach (Service item in myServices.ServiceItems) 12 { 13 Console.WriteLine("ServiceName:{0} Impl:{1} Inter:{2}", item.ServiceName, item.Impl, item.Inter); 14 } 15 } 16 } 17 18 #region[自定义Section处理代码] 19 public class Service : ConfigurationElement 20 { 21 #region[当遇不能识别的元素时不让程序报错] 22 protected override bool OnDeserializeUnrecognizedAttribute(string name, string value) 23 { 24 return true; 25 } 26 protected override bool OnDeserializeUnrecognizedElement(string elementName, System.Xml.XmlReader reader) 27 { 28 return true; 29 } 30 #endregion 31 32 #region[节点元素] 33 [ConfigurationProperty("ServiceName", IsRequired = true)] 34 public string ServiceName 35 { 36 get { return this["ServiceName"].ToString(); } 37 } 38 39 [ConfigurationProperty("Impl", IsRequired = true)] 40 public string Impl 41 { 42 get { return this["Impl"].ToString(); } 43 } 44 45 [ConfigurationProperty("Inter", IsRequired = true)] 46 public string Inter 47 { 48 get { return this["Inter"].ToString(); } 49 } 50 #endregion 51 } 52 53 public class Services : ConfigurationElementCollection 54 { 55 protected override ConfigurationElement CreateNewElement() 56 { 57 return new Service(); 58 } 59 protected override object GetElementKey(ConfigurationElement element) 60 { 61 return ((Service)element).ServiceName; 62 } 63 } 64 65 public class ServicesSection : ConfigurationSection 66 { 67 [ConfigurationProperty("services", IsDefaultCollection = false)] 68 public Services ServiceItems { get { return (Services)base["services"]; } } 69 } 70 #endregion 71 }
效果:
本小节代码下载
三、参考资料