configsections configuration of C # configuration files

Source: Internet
Author: User
Tags connectionstrings

For small projects, configuration information can be configured through appsettings, and if the configuration information is too much, appsettings seems a bit messy, and when the developer calls, is not friendly, the node name is easy to write wrong, at this time, we have several solutions

1 Develop a configuration information persistence class for managing configuration information and providing object-oriented support2 using the. NET configsections, the configuration information is chunked managed, and the entity classes are provided, making it easy for developers to use it

This article is mainly about the second scenario, which consists of the entity class, the entity class factory and the configuration file three parts, see the code:

Entity class Design:

1 namespaceConfiger2 {3     /// <summary>4     ///Site information Configuration node5     /// </summary>6      Public classwebconfigsection:configurationsection7     {8         /// <summary>9         ///Site nameTen         /// </summary> One[ConfigurationProperty ("WebName", DefaultValue ="", isrequired =true, IsKey =false)] A          Public stringWebName -         { -  the             Get{return(string) This["WebName"]; } -             Set{ This["WebName"] =value;} -         } -         /// <summary> +         ///website domain name -         /// </summary> +[ConfigurationProperty ("DoMain", DefaultValue ="", isrequired =true, IsKey =false)] A          Public stringDoMain at         { -  -             Get{return(string) This["DoMain"]; } -             Set{ This["DoMain"] =value;} -         } -  in     } -}

Entity factory class design, mainly used to produce entity configuration information

1 namespaceConfiger2 {3     /// <summary>4     ///Site configuration Information Factory5     /// </summary>6      Public classWebconfigmanager7     {8         /// <summary>9         ///Configuration Information EntityTen         /// </summary> One          Public Static ReadOnlyWebconfigsection Instance =GetSection (); A  -         Private Staticwebconfigsection GetSection () -         { thewebconfigsection config = configurationmanager.getsection ("webconfigsection") aswebconfigsection; -             if(config = =NULL) -                 Throw Newconfigurationerrorsexception (); -             returnconfig; +         } -     } +}

And finally, the. config file, which has configsections and the specified sections block, it is important to note that the configsections must be in the first position of the configuration

1 <?XML version= "1.0" encoding= "Utf-8"?>2 <Configuration>3   <configsections>4     < Sectionname= "Webconfigsection"type= "configer.webconfigsection, test"/>5   </configsections>6   <connectionStrings>7     <Addname= "Backgroundentities"connectionString= "metadata=res://*/model1.csdl|res://*/model1.ssdl|res://*/model1.msl;provider=system.data.sqlclient; Provider connection String=&quot;data source=.\sqlexpress;initial catalog=background;integrated Security=True; multipleactiveresultsets=true&quot; "ProviderName= "System.Data.EntityClient" />8   </connectionStrings>9 Ten   <webconfigsectionWebName= "Occupy Site"DoMain= "Www.zhanzhan.com"  /> One   <appSettings> A     <AddKey= "Site"value= "Www.zzl.com"/> -  -   </appSettings> the </Configuration>

After the implementation of the above three steps, we can call it, hehe

1 Static void Main (string[] args)2   {3      Console.WriteLine ( system.configuration.configurationmanager.appsettings["site"]); 4      Console.WriteLine (WebConfigManager.Instance.DoMain); 5      Console.WriteLine (WebConfigManager.Instance.WebName); 6    }   

configsections configuration of C # configuration files

Related Article

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.