C # questions about the App. config file under multiple assemblies in the project

Source: Internet
Author: User

Apps are often used in projects. some of the config files are automatically generated, such as those generated when the webservice or wcf Service is referenced, and some manually created configuration files are directly named app by default. config. Some of these configurations save some variables used by the current Assembly for external changes, such: [csharp] <configuration> <appSettings> <add key = "keyName" value = "value"/> </appSettings> </configuration> <appSettings> <add key = "keyName" value = "value"/> </appSettings> </configuration> directly uses ConfigurationManager. appSettings ["key name"] is easier to read. For example: [csharp] public class ReadConfig {public static string ConfigKeyValue {string config = ConfigurationManager. etettings ["ConfigKeyValue"]; config = string. IsNullOrEmpty (config )? "Empty string": config; return config;} public class ReadConfig {public static string ConfigKeyValue {string config = ConfigurationManager. appSettings ["ConfigKeyValue"]; config = string. isNullOrEmpty (config )? "Null String": config; return config;} indicates database connection. For example, configuration is generated when ADO. NET Entity is used to connect data. [Csharp] <? Xml version = "1.0" encoding = "UTF-8"?> <Configuration> <startup> <supportedRuntime version = "v4.0" sku = ". NETFramework, Version = v4.0 "/> </startup> <connectionStrings> <add name =" OracleEntities "connectionString =" "/> </connectionStrings> </configuration> <? Xml version = "1.0" encoding = "UTF-8"?> <Configuration> <startup> <supportedRuntime version = "v4.0" sku = ". NETFramework, Version = v4.0 "/> </startup> <connectionStrings> <add name =" OracleEntities "connectionString =" "/> </connectionStrings> </configuration> has Microsoft. practices. enterpriseLibrary configuration (http://blog.csdn.net/yysyangyangyangshan/article/details/8488791) for connecting to the database ). In addition, the hybrid mode assembly is generated for the runtime version of v2.0.50727. It cannot be used when the 4.0 running error is not configured with other information, [csharp] <? Xml version = "1.0"?> <Configuration> <startup useLegacyV2RuntimeActivationPolicy = "true"> <supportedRuntime version = "v4.0" sku = ". NETFramework, Version = v4.0 "/> <supportedRuntime version =" v2.0.50727 "/> </startup> </configuration> <? Xml version = "1.0"?> <Configuration> <startup useLegacyV2RuntimeActivationPolicy = "true"> <supportedRuntime version = "v4.0" sku = ". NETFramework, Version = v4.0 "/> <supportedRuntime version =" v2.0.50727 "/> </startup> </configuration>. config has many functions. But what should I do if app. config is used in many Assembly sets in a project? For example, in the FouctionDll program configuration: [csharp] <? Xml version = "1.0" encoding = "UTF-8"?> <Configuration> <appSettings> <add key = "key1" value = "Hello, world! "/> </AppSettings> </configuration> <? Xml version = "1.0" encoding = "UTF-8"?> <Configuration> <appSettings> <add key = "key1" value = "Hello, world! "/> </AppSettings> </configuration> configuration in the main directory TestAppConfig: [csharp] <? Xml version = "1.0" encoding = "UTF-8"?> <Configuration> <appSettings> <add key = "key2" value = "Hello, world! "/> </AppSettings> </configuration> <? Xml version = "1.0" encoding = "UTF-8"?> <Configuration> <appSettings> <add key = "key2" value = "Hello, world! "/> </Appsetdll> </configuration> If the main assembly needs to reference FouctionDll, the configuration is copied because the configuration name is repeated, it will naturally be overwritten by the configuration of the main program. In addition, if you want to reference the remote service in FouctionDll, app. config is automatically generated. Once the main program references this configuration, it is still unavailable. To solve this problem, we should do the following: 1. In the first case, if the configuration of each Assembly is manually added, the name will change. The read method does not use ConfigurationManager. appSettings ["key"], which can be changed to [csharp] public class ReadConfig {private static string currentConfig = System. appDomain. currentDomain. setupInformation. applicationBase + @ "ConfigName. config "; // <summary> /// determine whether there is an operation interval /// </summary> public static string ConfigKeyValue {get {string time = GetAttributeValue (currentConfig, "ConfigKeyValue"); if (string. isNullOrEmpty (tim E) {return "180";} return time ;}} /// <summary> /// obtain the properties of the configuration file /// </summary> /// <param name = "key"> </param> // <returns> </returns> private static string GetAttributeValue (string file, string key) {string value = string. empty; try {if (File. exists (file) {XmlDocument xml = new XmlDocument (); xml. load (file); XmlNode xNode = xml. selectSingleNode ("// appSettings"); XmlElement element = (XmlEle Ment) xNode. selectSingleNode ("// add [@ key = '" + key + "']"); value = element. getAttribute ("value "). toString () ;}} catch {} return value ;}} public class ReadConfig {private static string currentConfig = System. appDomain. currentDomain. setupInformation. applicationBase + @ "ConfigName. config "; // <summary> /// determine whether there is an operation interval /// </summary> public static string ConfigKeyValue {get {string time = GetA TtributeValue (currentConfig, "ConfigKeyValue"); if (string. isNullOrEmpty (time) {return "180" ;}return time ;}} /// <summary> /// obtain the properties of the configuration file /// </summary> /// <param name = "key"> </param> // <returns> </returns> private static string GetAttributeValue (string file, string key) {string value = string. empty; try {if (File. exists (file) {XmlDocument xml = new XmlDocument (); xml. load (file); XmlNode XNode = xml. selectSingleNode ("// appSettings"); XmlElement element = (XmlElement) xNode. selectSingleNode ("// add [@ key = '" + key + "']"); value = element. getAttribute ("value "). toString () ;}} catch {} return value ;}the advantage of this method is that each assembly is relatively independent, the disadvantage is that if it is an automatically generated app. config still has the above problem. What if the name must be app. config? You can use hyperlinks, that is, only the main program in the project uses the app. config, other Assembly uses its link, so that it can be used together to read or use ConfigurationManager in the current directory. appSettings ["keyName"]; the advantage of this method is that it can solve the problem that several Assemblies share an app. the disadvantage of config is that the Assembly is not independent. Because the same file is referenced, the Assembly needs to be re-checked and then manually referenced in the moving directory. In short, the first purpose of software development is that the software functions normally, and the second is that we should make ourselves as convenient as possible during development. Only more convenient and efficient can we improve.

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.