/// <summary> /// 重写基类OnCreate方法来创建本地配置实例 /// </summary> /// <param name="sectionName">节点名称</param> /// <param name="type">The type.</param> /// <param name="major">主版本号</param> /// <param name="minor">次版本号</param> /// <returns></returns> protected override object OnCreate(string sectionName, Type type, out int major, out int minor) { major = XmlSerializerSectionHandler.GetConfigurationClassMajorVersion(type); minor = XmlSerializerSectionHandler.DefaultUninitMinorVersion; string configPath = GetConfigSectionFileName(sectionName); if (configPath.Length == 0) { return(null); } object retVal; try { XmlDocument doc = new XmlDocument(); doc.Load(configPath); //初始化配置实例 retVal = XmlSerializerSectionHandler.GetConfigInstance(doc.DocumentElement, type, out major, out minor); // XmlSerializerSectionHandler.GetConfigVersion( doc.DocumentElement , out major , out minor ); } catch (Exception ex) { HandleException(ex, "创建本地配置时出错: sectionName=" + sectionName + ",type=" + type.Name + ", 会创建空实例替代", sectionName); //出现异常通过反射初始化为空配置实例 retVal = Activator.CreateInstance(type); } //为配置文件添加FileWatcher ConfigWatcher.SetupWatcher(configPath, retVal); return(retVal); }
/// <summary> /// 验证远程配置文件的有效性 /// </summary> /// <param name="sectionName">Name of the section.</param> /// <param name="stream">The stream.</param> private void CheckDownloadStream(string sectionName, Stream stream) { ConfigEntry entry = this.GetEntry(sectionName); if (entry == null) { throw new System.Configuration.ConfigurationErrorsException("在 RemoteConfigurationManager 没有找到 '" + sectionName + "' "); } XmlDocument doc = new XmlDocument(); doc.Load(stream); XmlSerializerSectionHandler.GetConfigInstance(doc.DocumentElement, entry.EntryType); }