飙血推荐
  • HTML教程
  • MySQL教程
  • JavaScript基础教程
  • php入门教程
  • JavaScript正则表达式运用
  • Excel函数教程
  • UEditor使用文档
  • AngularJS教程
  • ThinkPHP5.0教程

ConfigurationManager姿势快闪-

时间:2022-06-10  作者:JulianHuang  

C# ConfigurationManager使用记录

最近一个祖传代码是使用.NET Fx写就的,我在使用控制台程序获取配置时有些折腾。

下面记录一些管理配置文件的姿势:

ConfigurationManager用于在客户机应用程序中获取配置信息;
对于web项目,请使用WebConfigurationManager类。

ConfigurationManager使用姿势

  1. 添加域名ig文件
<configuration>
   <appSettings>
		<add key="ProjectName" value="域名.productcenter" />
	 </appSettings>
		<connectionStrings>
				<add name="DBConnection" connectionString="Data Source=(LocalDB)\v11.0;Initial Catalog=WingtipToys;Integrated Security=True;Pooling=False"/>
		</connectionStrings>
</configuration>
  1. 注意:编译之后域名ig配置节会进入可执行文件的配置文件域名ig
  2. 域名ettings["key1"]
    域名ectionStrings["DBConnection"] 用于从应用的默认配置中获取程序配置、连接字符串配置, 这也是ConfigurationManager最常规的用法。
  1. 如何读取外部配置?
    将所有配置信息放在一个配置文件显得非常混乱,特别是[密码管理]的时候, 可能会划分多个配置文件。
    ConfigurationManager支持项目中创建另外的配置文件。
  ------ 域名ig文件-----
<configuration>
	<connectionStrings configSource="域名ig" />
</configuration>

----- 域名ig文件, 这里已经不需要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>

附加的这个文件不会进域名ig文件,可以想象到,当需要隐藏该文件配置,可以不把该文件加入代码管理。

  1. ConfigurationManager支持Machine,User,Exe三个级别的配置文件, 可以通过ExeConfigurationFileMap加载特定位置的配置文件。
    var configFileMap = new ExeConfigurationFileMap()
    {
        ExeConfigFilename = @"E:\Test\WpfApp2\bin\Debug\域名ig"
    };
    var v = 域名MappedExeConfiguration(configFileMap, 域名);

我们顺便看下微软是如何编写工具库文件,ConfigurationManager 是一个静态类,静态构造函数,

在使用静态方法 AppSettings["key1"]索引配置时,必须先确保配置文件已经就绪,注意下面的PrepareConfigSystem==>EnsureConfigurationSystem方法

      public static object GetSection(string sectionName)
        {
            if (域名llOrEmpty(sectionName))
            {
                return null;
            }

            PrepareConfigSystem();
            return 域名ection(sectionName);
        }
        
          private static void PrepareConfigSystem()
        {
            if (s_initState < 域名le)
            {
                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 < 域名le) {
                    s_initState = 域名ted;
                    try {
                        try {
                            s_configSystem = new ClientConfigurationSystem();
                            s_initState = 域名le;
                        }
                        catch (Exception e) {
                            s_initError = new ConfigurationErrorsException(域名tring(域名ig_client_config_init_error), e);
                            throw s_initError;
                        }
                    }
                    catch {
                        s_initState = 域名leted;
                        throw;
                    }
                }
            }
        }

本文算是简短的技术快闪,记录了ConfigurationManager 的使用姿势和微软工具库的一般开发模式。

标签:编程
湘ICP备14001474号-3  投诉建议:234161800@qq.com   部分内容来源于网络,如有侵权,请联系删除。