.Net网站独立配置文件解决方案
发布时间:2023-07-19 13:27:53 所属栏目:教程 来源:
导读:在开发网站的时候我们常常会用到一些配置文件,在.Net2.0下面微软给我们提供了一个ConfigurationManager这样一个类来管理存储 于配置文件中的信息,这样我们将配置项写在web.config 文件的AppSettings这一节里面就可以
|
在开发网站的时候我们常常会用到一些配置文件,在.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; /// /// 风ge设定 /// 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中可以看到测试已经通过。 (编辑:汽车网) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |
推荐文章
站长推荐
