C # Read the custom nodes in Appconfig,
When using Nlog today, I found a problem that I did not pay attention to before.
In the past, my app configuration files were written in this way. Of course, xml will be used when there are many configurations.
If there is a lot of content in the <deleetask> node, I sometimes cannot tell which one is doing anything. Maybe my friends will say that you just need to add a comment. But can I put some identical configurations together, just like the nlog above. First try to modify the configuration file
1 <configSections>2 <section name="mySection" type="ConfigSolution.ConfigSectionHandler,ConfigSolution"></section>3 </configSections>4 <mySection>5 <port CPort="40001" WPort="40002" SPort="50000"></port>6 <coustomAssembly CommandsAssembly="HX.Components.Command.Collection" CommandMessagesAssembly="HX.Components.CommandMessage.Collection"></coustomAssembly>7 </mySection>
So how can we get the value in section? From the configSections element to the online storm. ConfigurationSection class
Then you can obtain the configuration file information through the GetSection method of the ConfigurationManager class. ( If the application needs to access its own configuration in read-only mode, the GetSection () overload method is recommended for Web applications. For client applications, the ConfigurationManager. GetSection method is recommended. ---- MSDN)
var mySection = ConfigurationManager.GetSection("mySection");
A try of running the program ushered in the first exception. System. Configuration. ConfigurationErrorsException: An error occurred while creating the Configuration section handler for mySection: The type "ConfigSolution. ConfigSectionHandler" is not inherited from "System. Configuration. IConfigurationSectionHandler. ---> System. TypeLoadException: The type "ConfigSolution. ConfigSectionHandler" is not inherited from "System. Configuration. IConfigurationSectionHandler.
Since my ConfigSolution. configSectionHandler is not from System. configuration. IConfigurationSectionHandler inheritance. Well, I will inherit it, and then look at what this interface has. Ctrl + T (SharpDevelop Shortcut Key), this interface is a method.
On MSDN, IConfigurationSectionHandler. Create does not have a large amount of information. Just one sentence: IConfigurationSectionHandler. Create method, Create a configuration section handler. Forget it. Let's track it through the breakpoint. Something really exists.
Well, the rest is to read the xml. Let's look at section return directly,
This time the program runs normally, and mySection also obtains the configuration file.
But how do we obtain the configuration data in the program? I created a MySectionHelper class to process the configuration file, which is roughly as follows:
1 public class MySectionHelper 2 { 3 readonly XmlNode _section; 4 readonly XmlNode _coustomAssembly; 5 public MySectionHelper(XmlNode section) 6 { 7 _section=section; 8 _coustomAssembly= _section.SelectSingleNode("coustomAssembly"); 9 }10 11 public string CommandsAssembly{get{return _coustomAssembly.Attributes["CommandsAssembly"].Value;}}12 }
I can't try it. My configuration file
1 <configSections>2 <section name="mySection" type="ConfigSolution.ConfigSectionHandler,ConfigSolution"></section>3 </configSections>4 <mySection>5 <port CPort="40001" WPort="40002" SPort="50000"></port>6 <coustomAssembly CommandsAssembly="HX.Components.Command.Collection" CommandMessagesAssembly="HX.Components.CommandMessage.Collection"></coustomAssembly>7 </mySection>
Running result:
Okay, everything is done. Then I went to the Internet to find out if I had more information.
You can see this article. Net custom application configuration.
Https://msdn.microsoft.com/zh-cn/sqlserver/ms228056 (v = vs.71). aspx