Asp.Net网站独立配置文件读写方案 |
| [ 作者:佚名 转贴自:网络转载 阅读次数:98 更新时间:2007-11-15 14:51:00 录入:刘光勇 ] 热 |
|
|
|
在开发网站的时候我们常常会用到一些配置文件,在.Net2.0下面微软给我们提供了一个ConfigurationManager这样一个类来管理存储于配置文件中的信息,这样我们将配置项写在web.config 文件的AppSettings这一节里面就可以使用键值直接来访问它了.就象这样System.Configuration.ConfigurationManager.AppSettings["MyNameIs"], 但是这里有个问题就是我们有一些设置希望在运行时可以通过网络访问的形式去作配置而不是手动修改web.config,但是ConfigurationManager并没有给我们提供一个方法来保存我们的设置(或者是我不知道,还有别的方法吧).还有一个原因是因为.Net运行框架会在应用程序启动后自动监视web.config,一旦这个文件被修改应用程序会自动重启这样的话我们的一些状态数据就会丢失,有没有更好的办法呢,其实是有的,我们的解决方案就是使用独立的可读写配置文件.先来看看代码吧.
using System; using System.Web; using System.IO; using System.Xml.Serialization;
namespace GB.Utility { /// /// 全站配置文件读写 /// [Serializable] public class SiteSettings { private static readonly string currentConfigFileName = "GBSettings.config";
#region PublicProperty #region AbsoluteWebRoot private string absoluteWebRoot; /// /// 站点的绝对Url引用 /// public string AbsoluteWebRoot { get { return absoluteWebRoot; } set { absoluteWebRoot = string.IsNullOrEmpty(value) ? string.Empty : value; } } #endregion
#region RelativeWebRoot private string relativeWebRoot; /// /// 站点的相对Url引用 /// public string RelativeWebRoot { get { return relativeWebRoot; } set { relativeWebRoot = string.IsNullOrEmpty(value) ? string.Empty : value; } } #endregion
#region DefaultPageSize private int defaultPageSize; /// /// 默认的分页大小 /// public int DefaultPageSize { get { return defaultPageSize; } set { defaultPageSize = value; } } #endregion
#region DefaultAbstractSize private int defaultAbstractSize; /// /// 默认的摘要长度值 /// public int DefaultAbstractSize { get { return defaultAbstractSize; } set { defaultAbstractSize = value; } } #endregion
#region Theme private string theme; /// /// 风格设定 /// public string Theme { get { return theme; } set { theme = value; } } #endregion #endregion
#region Common Members
#region Private Members private static string _configurationFilePath = System.IO.Path.Combine( System.AppDomain.CurrentDomain.BaseDirectory.Replace("/", System.IO.Path.DirectorySeparatorChar.ToString()), currentConfigFileName); private static SiteSettings _currentConfiguration; #endregion
#region Constructor private SiteSettings() { } #endregion
public static SiteSettings Instance { get { if (_currentConfiguration == null) { Load(); if (_currentConfiguration == null) _currentConfiguration = new SiteSettings(); }
return _currentConfiguration; } }
private static void Load() {
if (!File.Exists(_configurationFilePath)) { throw new Exception("Can't locate config file! Check the config file path."); _currentConfiguration = new SiteSettings(); } else { XmlSerializer ser = new XmlSerializer(typeof(SiteSettings)); StreamReader reader = new StreamReader(_configurationFilePath);
// XML反序列化. _currentConfiguration = (SiteSettings)ser.Deserialize(reader);
reader.Close();
}
}
private static void Load(string configFile)//这个方法如果改为公开方案那么可以在运行时来读取合适的配置文件,这样就避免在布署时修改多个功能项. { _configurationFilePath = configFile; Load(); }
public static void Save() { XmlSerializer ser = new XmlSerializer(typeof(SiteSettings)); FileStream fileOut;
if (!File.Exists(_configurationFilePath)) { fileOut = new FileStream(_configurationFilePath, FileMode.CreateNew, FileAccess.ReadWrite, FileShare.ReadWrite); } else { fileOut = new FileStream(_configurationFilePath, FileMode.Truncate, FileAccess.ReadWrite, FileShare.ReadWrite); }
// Serialize the object in XML to the given file stream. ser.Serialize(fileOut, _currentConfiguration);
// Close the file stream. fileOut.Close(); } private static string GetBaseDirectory() { return System.AppDomain.CurrentDomain.BaseDirectory; } #endregion } } 这样我们在站点根目录下面建立一个名为"GBSettings.config"的配置文件就可以对它进行读写操作了.
它里面的内容:
Http://Localhost/ GBWeb/ 20 200 default
大家注意到这里面的字段和我们建立的类里面的公开属性是一一对应的, 因此如果需要增加新的可配置项目只要添加相关的公开属性即可,其它的公共代码部分就不用动了, 这个配置文件类使用单例模式一次性加载文件,在访问速度上是足够快了.并且在配置文件修改的时候不会引起应用程序的重起,只需要调用Save()方法即可生效, 真是非常方便呀.
还没有完?对,还有一个问题,这样的代码是可测试的吗,当然, 这里以NUnit为例,在测试时只需要将配置文件拷贝至测试工程的根目录,注意这里的生成选项使用复制到输出目录,在文件较新时复制. 当然,web.cofig也要拷贝至测试工程根目录下面并改名为 测试工程名称.dll.config 就可以了. 测试代码如下:
[Test] public void ConfigFileTest() { SiteSettings.Instance.Theme = "default"; Assert.IsTrue(SiteSettings.Instance.Theme.Equals("default", StringComparison.InvariantCultureIgnoreCase)); SiteSettings.Instance.Theme = "blue"; SiteSettings.Save(); Assert.IsTrue(SiteSettings.Instance.Theme.Equals("blue", StringComparison.InvariantCultureIgnoreCase)); }在NUnit中可以看到测试已经通过.
|
|
|
|