我们在设计与实现一个应用程序时,往往会将一些常量写入配置文件当中,并且希望这些常量作为一个结构体被整体读写。对此,C#提供的System.Net.Configuration可以帮我们轻松的实现这个需求。本文将介绍几种常见的使用常见及实现,样例代码为.NET framework 4.6.1的控制台程序(如ConfigDemo),配置文件名为App.config。
1. Key-Value配置项
对于一些简单的配置项,我们可以直接将其存储在configuration文件中提供的appSettings当中,该配置段中可存储一系列的键值对配置项,如:
<configuration>
...
<appSettings>
<add key="proxy" value="http://example:12345" />
</appSettings>
...
</configuration>
之后,通过制定Key的值,利用ConfigurationManager读取appSettings中所需的Value,如:
var proxyUrl = ConfigurationManager.AppSettings.Get("proxy");
2. 自定义结构化配置项
对于某些相关联的配置,为了便于管理及使用,我们希望将其放在一个结构体中,并令其作为一个整体被读入应用程序。假设我们在启动应用程的某项服务时,需要提供一个固定的代理服务器地址,此时我们将该信息放置一个自定义的结构体中,以便后续扩展,如:
namespace ConfigDemo
{
public class MyServiceConfiguration : ConfigurationSection
{
private const string SectionName = "myService";
private const string ProxyProp = "proxy";
public static readonly MyServiceConfiguration Default;
static MyServiceConfiguration()
{
Default = (MyServiceConfiguration)ConfigurationManager.GetSection(SectionName);
}
[ConfigurationProperty(ProxyProp, IsRequired = true)]
public string Proxy
{
get { return (string)this[ProxyProp]; }
set { this[ProxyProp] = value; }
}
}
}
在配置文件中,我们也需同样将其视为一个整体写入,如:
<configuration>
<configSection>
<section name="myService" type="ConfigDemo.MyServiceConfiguration, ConfigDemo"/>
</configSection >
<myService proxy="http://example:12345" />
...
</configuration>
3. 自定义数组类结构配置项
有些时候我们需要一系列相同结构,但值不同的配置项,即类似数组结构的配置项。此时,除了ConfigraionSection,我们还需用到ConfigurationElement和ConfigurationCollection,如:
//ConfigurationElement
namespace ConfigDemo.Configurations
{
public class MyProxy : ConfigurationElement
{
private const string EnvProp = "env";
private const string ProxyProp = "proxy";
[ConfigurationProperty(EnvProp, IsKey = true, IsRequired = true)]
public string Env
{
get { return (string)this[EnvProp]; }
set { this[EnvProp] = value; }
}
[ConfigurationProperty(ProxyProp, IsRequired = true)]
public string Proxy
{
get { return (string)this[ProxyProp]; }
set { this[ProxyProp] = value; }
}
}
}
//ConfigurationElementCollection
namespace ConfigDemo.Configurations
{
public class MyProxyCollection : ConfigurationElementCollection
{
// <inheritdoc />
protected override ConfigurationElement CreateNewElement() => new MyProxy();
// <inheritdoc />
protected override object GetElementKey(ConfigurationElement element) => ((MyProxy)element).Env;
}
}
//ConfigurationSection
namespace ConfigDemo.Configurations
{
public class MyProxyConfiguration : ConfigurationSection
{
private const string MyProxiesProp = "myProxies";
[ConfigurationProperty(MyProxiesProp, IsRequired = true)]
public MyProxyCollection MyProxies
{
get { return (MyProxyCollection)base[MyProxiesProp]; }
set { base[MyProxiesProp] = value; }
}
}
}
此处,我们在实现一个单独辅助类来读取上述配置,以代码更加简洁清晰,如:
//ConfigurationSection
namespace ConfigDemo
{
public class MyProxyHelper
{
private const string SectionName = "myProxyHelper";
private static readonly List<MyProxy> Proxies;
static MyProxyHelper()
{
var config = (MyProxyConfiguration)ConfigurationManager.GetSection(SectionName);
Proxies = config.MyProxies.Cast<MyProxy>().ToList();
}
public string GetByEnv(string env) => Proxies.SingleOrDefualt(x => x.Env == env);
}
}
在配置文件中,我们也需同样将其视为一个数组整体写入,如:
<configuration>
<configSection>
<section name="myProxyHelper" type="ConfigDemo.Configurations.MyProxyConfiguration, ConfigDemo"/>
</configSection >
<myProxyHelper>
<myProxies>
<add env="dev" proxy="http://exampedev:12345" />
<add env="fat" proxy="http://exampeuat:12345" />
</myProxies>
</myProxyHelper>
...
</configuration>
4725



被折叠的 条评论
为什么被折叠?



