ConfigurationManager姿勢快閃

C# ConfigurationManager使用記錄

最近一個祖傳代碼是使用.NET Fx寫就的,我在使用控制檯程序獲取配置時有些折騰。

下面記錄一些管理配置文件的姿勢:

ConfigurationManager用於在客戶機應用程序中獲取配置信息;
對於web項目,請使用WebConfigurationManager類。

ConfigurationManager使用姿勢

  1. 添加app.config文件
<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. 注意:編譯之後app.config配置節會進入可執行文件的配置文件Demo.exe.config
  2. ConfigurationManager.AppSettings["key1"]
    ConfigurationManager.ConnectionStrings["DBConnection"] 用於從應用的默認配置中獲取程序配置、連接字符串配置, 這也是ConfigurationManager最常規的用法。

  1. 如何讀取外部配置?
    將所有配置信息放在一個配置文件顯得非常混亂,特別是[密碼管理]的時候, 可能會劃分多個配置文件。
    ConfigurationManager支持項目中創建另外的配置文件。
  ------ app.config文件-----
<configuration>
	<connectionStrings configSource="DBConnectionStrings.config" />
</configuration>

----- DBConnectionString.config文件, 這裏已經不需要configuration頂級配置節---- 
<?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>

附加的這個文件不會進Demo.exe.config文件,可以想象到,當需要隱藏該文件配置,可以不把該文件加入代碼管理。

  1. ConfigurationManager支持Machine,User,Exe三個級別的配置文件, 可以通過ExeConfigurationFileMap加載特定位置的配置文件。
    var configFileMap = new ExeConfigurationFileMap()
    {
        ExeConfigFilename = @"E:\Test\WpfApp2\bin\Debug\PositionConfig.config"
    };
    var v = ConfigurationManager.OpenMappedExeConfiguration(configFileMap, ConfigurationUserLevel.None);

我們順便看下微軟是如何編寫工具庫文件,ConfigurationManager 是一個靜態類,靜態構造函數,

在使用靜態方法 AppSettings["key1"]索引配置時,必須先確保配置文件已經就緒,注意下面的PrepareConfigSystem==>EnsureConfigurationSystem方法

      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;
            }
        }

使用了一個狀態字段來表徵初始化過程, 注意這裏使用了一個lock防止併發下被多次初始化

     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;
                    }
                }
            }
        }

本文算是簡短的技術快閃,記錄了ConfigurationManager 的使用姿勢和微軟工具庫的一般開發模式。

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章