Early in the morning, the company was used to quickly browse the news. North Korea announced that it was ready to prepare for a missile attack and the Sino-Japanese Diaoyu Islands incident ..., I was angry with my colleagues and thought of the tomb of a European Bishop. He said: When I was young, I thought I could change the world, but as I got older, I found that I do not have this ability, so I decided that I only change my country, but I cannot change my country. When I was old, I just wanted to change my family, but I realized that my family was not willing to be changed, so I suddenly realized that if I changed myself ......
Since the configuration file optimization solution in the previous project, I felt that it was not in line with the current business, so under the personal guidance of the leaders, I had this configuration file optimization solution (2 ), this section describes a configuration file plug-in configurationsectiondesigner in Visual Studio 2010, which is based on.. Net Configuration System. Download and install the plug-in. You can also find the source code of the plug-in on the Internet. If you are interested, you can also study it.
Next, we will introduce the use of the plug-in step by step and accumulate knowledge for ourselves. We need to display configuration files in modules and categories ,.. Net configuration system can be a section module. You can refer to the configuration structure of WCF. For example, for the following configuration, XML parsing is usually used, which is very inconvenient. We use this plug-in to put them in a section, which is also very convenient to use.
Configuration item before optimization
View code
1 <add key="localIPV4Scope" value="10."/> 2 <add key="tcpCommTimeout" value="60000"/> 3 <add key="localIPV4Scope" value="10."/> 4 <add key="encoding" value="UTF-8"/> 5 <add key="output" value="C:\"/> 6 <add key="style" value="Style1"/> 7 <mainFrameService> 8 <service name="SPD0" channel="SPDTCP" server="192.168.1.100:6005" servicetype=""/> 9 <service name="SPD1" channel="SPDTCP" server="192.168.1.101:6005" servicetype=""/>10 <service name="SPD2" channel="SPDTCP" server="192.168.1.102:8991" servicetype=""/>11 </mainFrameService>12 </appSettings>
Configuration items after optimization
View code
1 <MyConfig localIPV4Scope="10." 2 tcpCommTimeout="60000" 3 description="MyConfig"> 4 <systemConfig encoding="UTF-8" 5 output="C:\" 6 style="Style1"/> 7 <mainFrameService> 8 <service name="SPD0" channel="SPDTCP" server="192.168.1.100:6005" servicetype=""/> 9 <service name="SPD1" channel="SPDTCP" server="192.168.1.101:6005" servicetype=""/>10 <service name="SPD2" channel="SPDTCP" server="192.168.1.102:8991" servicetype=""/>11 </mainFrameService>12 </MyConfig>
1. First add new item, find the built-in template of the plug-in just installed, and name it myconfig. CSD.
2. Create a configuration item model. the Tollbox on the left of the csd file contains related controls. First, drag a ConfigurationSection and name it MyConfig. Add three attributes respectively. On the Attribute page, select the Type of the Attribute, add two new elements (SystemConfig and MainFrameService). For SystemConfig, drag a ConfigurationElement to name it SystemConfigElement, add three corresponding attributes, and select the corresponding Type, then, specify the SystemConfig Type as SystemConfigElement, and associate it with each other. A pointing arrow is displayed. Then MainFrameService is used. This node is a collection, so you need to drag a ConfigurationElementCollection to name it MainFrameServiceCollection, drag another ConfigurationElement to name it Service, and add the corresponding Attribute and the specified Type. in this way, set MainFrameService's Type to MainFrameServiceCollection, and MainFrameServiceCollection's Item Type to Service. The configuration model is successfully created. For example, the corresponding configuration item code is generated during saving.
3. Through the above work, we have established the basic configuration structure. The following is the configuration implementation result, as shown below:
View code
1 <configSections> 2 <section name="myConfig" type="ConfigDesigner.MyConfig, ConfigDesigner"/> 3 </configSections> 4 <myConfig localIPV4Scope="10." 5 tcpCommTimeout="60000" 6 description="MyConfig"> 7 <systemConfig encoding="UTF-8" 8 output="C:\" 9 style="Style1"/>10 <mainFrameService>11 <service name="SPD0" channel="SPDTCP" server="192.168.1.100:6005" servicetype=""/>12 <service name="SPD1" channel="SPDTCP" server="192.168.1.101:6005" servicetype=""/>13 <service name="SPD2" channel="SPDTCP" server="192.168.1.102:8991" servicetype=""/>14 </mainFrameService>15 </myConfig>
In this way, even if there are many configuration modules and the config file is clear, you can use the configSource attribute to configure multiple external configuration files separately. It is easy to use, as shown below:
View code
1 var localIPV4Scope = MyConfig.Instance.LocalIPV4Scope; 2 var tcpCommTimeout = MyConfig.Instance.TcpCommTimeout; 3 var encoding = MyConfig.Instance.SystemConfig.Encoding; 4 var output = MyConfig.Instance.SystemConfig.Output; 5 var style = MyConfig.Instance.SystemConfig.Style; 6 Console.WriteLine("localIPV4Scope:{0}",localIPV4Scope); 7 Console.WriteLine("tcpCommTimeout:{0}", tcpCommTimeout); 8 Console.WriteLine("encoding:{0}", encoding); 9 Console.WriteLine("output:{0}", output);10 Console.WriteLine("style:{0}", style);11 12 var mainFrameService = MyConfig.Instance.MainFrameService;13 foreach (Service service in mainFrameService)14 {15 Console.WriteLine("name:{0},channel:{1},server:{2},servicetype:{3}", service.Name, service.Channel, service.Server, service.Servicetype);16 }17 18 Console.ReadLine();
Focus on details and start from changing yourself