C # Read the custom nodes in AppConfig

Source: Internet
Author: User

Today, when using Nlog, I found a problem that I didn't notice before.

Previously, my app configuration files were written in this way, and of course the configuration was more often used instead of XML.

If there is a lot of content in the <appSettings> node, I sometimes don't know which is what to do, perhaps friends will say, you add a comment on the line. But can you put some of the same configuration together, just like the nlog above. Try to retrofit the configuration file first.

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 do you get the value in section? From the beginning of the configsections element to the online storm. ConfigurationSection class

  You then know that you can obtain information about the configuration file through the GetSection method of the ConfigurationManager class. (getsect Ion () overloaded methods in the case of WEB applications, Or the configurationmanager. GetSection method in the case of the client applications. " > If your application requires read-only access to its own configuration, it is recommended to use the GetSection () overloaded method for Web applications, and for client applications, it is recommended that you use ConfigurationManager . GetSection the method. ----MSDN )

var mysection = configurationmanager.getsection ("mysection");

run the program to try and usher in the first exception. System.Configuration.ConfigurationErrorsException: Error creating configuration section handler for MySection: Type " Configsolution.configsectionhandler "does not inherit from" System.Configuration.IConfigurationSectionHandler ". ---> system.typeloadexception: Type "Configsolution.configsectionhandler" not from " System.Configuration.IConfigurationSectionHandler "inheritance.

since my configsolution.configsectionhandler does not inherit from System.Configuration.IConfigurationSectionHandler, then I will inherit it and see what this interface is all about. Things, Ctrl+t (sharpdevelop shortcut keys), this interface is a method

Direct MSDN, IConfigurationSectionHandler. Create information is not very large, just a word: IConfigurationSectionHandler.Create method, creating a configuration section handler. Forget it, direct breakpoint tracking, there is something

Well, the rest is the reading of the XML. Look directly at the section return,

This time the program is working properly, and MySection also got the configuration file

But how do we get these configuration data in the program? I created a Mysectionhelper class that handles the configuration file, roughly as follows

1      Public classMysectionhelper2     {3         ReadOnlyXmlNode _section;4         ReadOnlyXmlNode _coustomassembly;5          Publicmysectionhelper (XmlNode section)6         {7_section=Section ;8_coustomassembly= _section. selectSingleNode ("coustomassembly");9         }Ten          One          Public stringcommandsassembly{Get{return_coustomassembly.attributes["commandsassembly"]. Value;}} A}

Try it. No, my profile

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>

Operation Result:

Well, it's all done. Then go to the Internet to find out if there are more information, sure enough

Zhi wrote an article on ". Net Custom application Configuration" You can go and see.

msdn:https://msdn.microsoft.com/zh-cn/sqlserver/ms228056 (v=vs.71). aspx

C # Read the custom nodes in AppConfig

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.