/// <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> /// 重写基类OnCreate方法来创建远程配置实例(原理其实还是把远程配置下载到本地创建本地配置实例) /// </summary> /// <param name="sectionName">Name of the section.</param> /// <param name="type">The type.</param> /// <param name="major">The major.</param> /// <param name="minor">The minor.</param> /// <returns></returns> protected override object OnCreate(string sectionName, Type type, out int major, out int minor) { string fileName = GetFileName(sectionName); //string path = GetPath( sectionName ); string path = ConfigUtility.Combine(config.LocalApplicationFolder, fileName); object obj = CreateLocalObject(type, path, out major, out minor); if (obj != null) { return(obj); } major = XmlSerializerSectionHandler.GetConfigurationClassMajorVersion(type); minor = XmlSerializerSectionHandler.DefaultUninitMinorVersion; try { RemoteConfigSectionParam newParams = GetServerVersion(sectionName, major); if (newParams != null) { //从远程下载! if (Download(newParams.SectionName, newParams.DownloadUrl, path, CheckDownloadStream)) { obj = CreateLocalObject(type, path, out major, out minor);//下载远程配置到本地后创建配置实例 } } } catch (Exception ex) { HandleException(ex, "从远程服务器下载配置 '" + sectionName + "' 时出错", sectionName); } //如果对象为null通过反射默认对象替代 if (obj == null) { LoggerWrapper.Logger.Info("无法从 RemoteConfiguration 取得拥有类型'" + type.Name + "' 的'" + sectionName + "' 节点,通将过反射创建空实例替代"); obj = Activator.CreateInstance(type); //为配置添加文件更改监控 ConfigWatcher.SetupWatcher(path, obj); ConfigWatcher.RegisterReloadNotification(path, OnConfigFileChanged); } return(obj); }
private object CreateLocalObject(Type type, string path, out int major, out int minor) { try { if (File.Exists(path)) { XmlDocument doc = new XmlDocument(); doc.Load(path); object obj = ConfigWatcher.CreateAndSetupWatcher(doc.DocumentElement, path, type, OnConfigFileChanged); XmlSerializerSectionHandler.GetConfigVersion(doc.DocumentElement, out major, out minor); return(obj); } } catch (Exception ex) { HandleException(ex, "RemoteConfigurationManager.CreateLocalObject,type=" + type.Name + ",path=" + path, type.Name); } major = XmlSerializerSectionHandler.GetConfigurationClassMajorVersion(type); minor = XmlSerializerSectionHandler.DefaultUninitMinorVersion; return(null); }