当前位置:网站首页>Configurationmanager pose flash

Configurationmanager pose flash

2022-06-09 10:39:00 Blog ape Majia brother

C# ConfigurationManager Use records

The most recent ancestral code is using .NET Fx Written , I had some trouble getting configuration using the console program .

Here are some gestures for managing profiles :

ConfigurationManager Used to get configuration information in the client application ;
about web project , Please use WebConfigurationManager class .

ConfigurationManager Using posture

  1. add to app.config file
<configuration>
   <appSettings>
		<add key="ProjectName" value="cvg.java.api.productcenter" />
	 </appSettings>
		<connectionStrings>
				<add name="DBConnection" connectionString="Data Source=(LocalDB)\v11.0;Initial Catalog=WingtipToys;Integrated Security=True;Pooling=False"/>
		</connectionStrings>
</configuration>
  1. Be careful : After the compilation app.config The configuration section will enter the configuration file of the executable Demo.exe.config
  2. ConfigurationManager.AppSettings["key1"]
    ConfigurationManager.ConnectionStrings["DBConnection"] Used to get the program configuration from the default configuration of the application 、 Connection string configuration , This is also ConfigurationManager The most common usage .

  1. How to read external configuration ?
    Putting all the configuration information in one configuration file is very confusing , especially [ Password management ] When , Multiple configuration files may be divided .
    ConfigurationManager Support the creation of additional configuration files in the project .
  ------ app.config file -----
<configuration>
	<connectionStrings configSource="DBConnectionStrings.config" />
</configuration>

----- DBConnectionString.config file ,  We don't need that anymore configuration Top level configuration section ---- 
<?xml version="1.0" encoding="utf-8"?>
<connectionStrings>
	<add name="DBConnection" connectionString="Data Source=(LocalDB)\v11.0;Initial Catalog=WingtipToys;Integrated Security=True;Pooling=False"  />
</connectionStrings>

The attached file will not go into Demo.exe.config file , You can imagine , When you need to hide the file configuration , It is not necessary to add this file to code management .

  1. ConfigurationManager Support Machine,User,Exe Three levels of configuration files , Can pass ExeConfigurationFileMap Load the configuration file for a specific location .
    var configFileMap = new ExeConfigurationFileMap()
    {
        ExeConfigFilename = @"E:\Test\WpfApp2\bin\Debug\PositionConfig.config"
    };
    var v = ConfigurationManager.OpenMappedExeConfiguration(configFileMap, ConfigurationUserLevel.None);

Let's take a look at how Microsoft writes tool library files ,ConfigurationManager Is a static class , Static constructor ,

Using static methods AppSettings["key1"] Index configuration , You must first make sure that the configuration file is ready , Note the following PrepareConfigSystem==>EnsureConfigurationSystem Method

      public static object GetSection(string sectionName)
        {
            if (string.IsNullOrEmpty(sectionName))
            {
                return null;
            }

            PrepareConfigSystem();
            return s_configSystem.GetSection(sectionName);
        }
        
          private static void PrepareConfigSystem()
        {
            if (s_initState < InitState.Usable)
            {
                EnsureConfigurationSystem();
            }

            if (s_initError != null)
            {
                throw s_initError;
            }
        }

A status field is used to represent the initialization process , Notice that a lock Prevent multiple initializations under concurrency

     private static void EnsureConfigurationSystem() {
            // If a configuration system has not yet been set, 
            // create the DefaultConfigurationSystem for exe's.
            lock (s_initLock) {
                if (s_initState < InitState.Usable) {
                    s_initState = InitState.Started;
                    try {
                        try {
                            s_configSystem = new ClientConfigurationSystem();
                            s_initState = InitState.Usable;
                        }
                        catch (Exception e) {
                            s_initError = new ConfigurationErrorsException(SR.GetString(SR.Config_client_config_init_error), e);
                            throw s_initError;
                        }
                    }
                    catch {
                        s_initState = InitState.Completed;
                        throw;
                    }
                }
            }
        }

This article is a brief technical flash , Recorded ConfigurationManager And the general development mode of Microsoft tool library .
,

原网站

版权声明
本文为[Blog ape Majia brother]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/160/202206090958018596.html