Enterprise Library: Configuration Application Block应用向导篇
Part 3
Written by: Rickie Lee (rickieleemail#yahoo.com)
My blog: www.cnblogs.com/rickie
Enterprise Library: Configuration Application Block应用向导篇, Part 1
Enterprise Library: Configuration Application Block应用向导篇, Part 2
******
(3)读取配置信息
使用Configuration Application Block最常用的方式是读取配置数据,如下Code Snippet演示应用程序如何读取Proxy配置信息到上述定义的ProxySettingsData类中。应用程序使用ConfigurationManager.GetConfiguration静态方法来检索配置信息。
private void btnReadConfigurationInfo_Click(object sender, System.EventArgs e)
{
// Using the static method, read the cached configuration settings
ProxySettingsData configData = ConfigurationManager.GetConfiguration("ProxySettings") as ProxySettingsData;
StringBuilder results = new StringBuilder();
results.Append("Configuration settings:");
results.Append(Environment.NewLine);
results.Append('\t');
results.Append(configData.ToString());
results.Append(Environment.NewLine);
txtConfigurationData.Text += results.ToString();
}
默认情况下,上述Configuration Console配置工具生成的ProxySettingsConfiguration.config为空白配置文件,没有包含任何配置信息。此时,如果读取配置信息,会抛出如下异常:
An unhandled exception of type 'System.Configuration.ConfigurationException' occurred in microsoft.practices.enterpriselibrary.configuration.dll
Additional information: The section name 'ProxySettings' could not be found in the Xml file D:\Temp\DemoCAB\bin\Debug\ProxySettingsConfiguration.config.
因此,需要先将配置信息写入ProxySettingsConfiguration.config配置文件,可以手工输入,也可通过如下程序代码自动写入配置信息。
(4)写配置信息
如下是ProxySettingsConfiguration.config配置文件:
<?xml version="1.0" encoding="utf-8"?>
<ProxySettings>
<xmlSerializerSection type="DemoCAB.ProxySettingsData, DemoCAB, Version=1.0.1862.33265, Culture=neutral, PublicKeyToken=null">
<ProxySettingsData xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<ProxyServer>proxy server</ProxyServer>
<Port>8080</Port>
</ProxySettingsData>
</xmlSerializerSection>
</ProxySettings>
元素<ProxySettings>与上述Configuration Console配置工具的Configuration Section名称一致,元素<xmlSerializerSection>包含type属性,该属性值DemoCAB.ProxySettingsData, DemoCAB, Version=1.0.1862.33265, Culture=neutral, PublicKeyToken=null 为配置信息反序列化时所用对象的全名称,反序列化对象为ProxySettingsData类型,配置信息以XML形式存放。
当你使用Configuration Application Block写配置数据时,上述定义的类ProxySettingsData在Runtime时实例化,将保存有配置数据。当你准备写数据时,传递ProxySettingsData对象到ConfigurationManager.WriteConfiguration方法,将配置数据写入XML配置文件。Code Snippet如下所示:
private void btnWriteConfigurationInfo_Click(object sender, System.EventArgs e)
{
ProxySettingsData configData = new ProxySettingsData();
configData.ProxyServer = txtProxyServer.Text.Trim();
configData.Port = Convert.ToInt32(txtPort.Text.Trim());
// Write the new configuration data to the XML file
ConfigurationManager.WriteConfiguration("ProxySettings", configData);
}
(5)缓存配置信息
ConfigurationBuilder类用来返回存储区中当前配置设置。基于性能的考虑,当每一个配置节信息从存储区读取后,ConfigurationBuilder将缓存该配置信息。只要缓存信息与底层存储区数据保持一致,缓存信息将返回给应用程序。当Storage Provider检测到配置设置发生变化时,它将触发一个事件,表示该配置节发生了变化。当ConfigurationBuilder接受到该事件时,将从缓存中清除该配置节信息。这样,在应用程序下次尝试读取该节配置信息时,ConfigurationBuilder将从存储区直接读取新的配置信息。
当使用XML文件的Storage Provider时,从配置文件发生改变到检测到该改变大约存在1500毫秒的延迟。ConfigurationChangeFileWatcher对象每1500毫秒轮询XML配置文件的变化,这不是一个可配置的设置。不过,你可以通过修改ConfigurationChangeFileWatcher源代码来改变轮询的频率。
另外,Client端程序也可以通过调用ConfigurationManager.ClearSingletonCache方法来清除缓存。
Code Snippet如下所示:
private void btnClearCache_Click(object sender, System.EventArgs e)
{
// Removes all sections from the internal cache.
ConfigurationManager.ClearSingletonSectionCache();
txtConfigurationData.Text += "The cache of configuration data has been cleared." + Environment.NewLine;
}
***
作者:Rickie Lee (rickieleemail#yahoo.com)
本文参考Enterprise Library, Configuration Application Block文档。
References:
1.
2. Rickie, Microsoft patterns & practices Enterprise Library January 2005 [中文稿], http://www.cnblogs.com/rickie/archive/2005/01/30/99443.html
3. Rickie, Enterprise Library released! http://www.cnblogs.com/rickie/archive/2005/01/29/99106.html