使用xxx.dll.config
如果发布的dll文件需要配置时,可以考虑把配置信息转到app.config,当然也可以不转移直接使用xxx.dll.config中的配置,当然需要保证程序执行目录下已经copy有xxx.dll.config文件了
代码入下,加入了文件监视缓存
View Code
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Configuration; using System.Reflection; using System.IO; using System.Web.Caching; using System.Web; namespace DotNet.A.Core { public sealed class ModelSetting { public static string GetSetting(string key) { return Assembly.GetExecutingAssembly().Location + ".config"; } public static string GetSetting(string name, string defV) { if (!Config.AppSettings.Settings.AllKeys.Contains(name)) return defV; return Config.AppSettings.Settings[name].Value; } public static string ModelConnString { get { return Config.ConnectionStrings.ConnectionStrings["DBSERP"].ConnectionString; } } public static string ConnString { get { return Config.ConnectionStrings.ConnectionStrings["ConnString"].ConnectionString; } } private static Configuration Config { get { var _Config = HttpRuntime.Cache["_Config"] as Configuration; if (_Config == null ) { var fileMap=new ExeConfigurationFileMap(); fileMap.ExeConfigFilename = Assembly.GetExecutingAssembly().Location + ".config"; _Config= ConfigurationManager.OpenMappedExeConfiguration(fileMap, ConfigurationUserLevel.None); var fileCacheDep = new CacheDependency(fileMap.ExeConfigFilename); System.Web.HttpRuntime.Cache.Add("_Config", _Config, fileCacheDep, Cache.NoAbsoluteExpiration, Cache.NoSlidingExpiration, CacheItemPriority.High, null); } return _Config; } } } }