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); XmlSerializerSectionHandler.GetConfigVersion(doc.DocumentElement, out major, out minor); } catch (Exception ex) { HandleException(ex, "Error when create local configuration: sectionName=" + sectionName + ",type=" + type.Name + ", create entry config instead", sectionName); //if exception here, return default configuration class and then setup watch retVal = Activator.CreateInstance(type); } XmlSerializerSectionHandler.SetupWatcher(configPath, retVal); return(retVal); }
protected RemoteConfigurationManager() : base() { string configFile = GetRemoteConfigFile(); try { config = XmlSerializerSectionHandler.CreateAndSetupWatcher <RemoteConfigurationManagerConfiguration>(configFile); if (config.CheckRemoteConfig) { System.Timers.Timer timer = new System.Timers.Timer(config.TimerInterval); timer.Elapsed += new System.Timers.ElapsedEventHandler(TimerCallback); timer.Start(); } } catch (Exception ex) { HandleException(ex, "Unabled to load RemoteConfigurationManager configuration file, Please set '" + RemoteConfigFileAppSettingKey + "' in appSettings", "RemoteConfigurationManager"); throw ex; } }
public static T GetSection <T>(string name, string path, out bool fromRemote) { if (System.IO.File.Exists(path)) { fromRemote = false; return(XmlSerializerSectionHandler.CreateAndSetupWatcher <T>(LocalConfigurationManager.MapConfigPath(path))); } else { fromRemote = true; return(RemoteConfigurationManager.Instance.GetSection <T>(name)); } }
void CheckDownloadStream(string sectionName, Stream stream) { ConfigEntry entry = this.GetEntry(sectionName); if (entry == null) { throw new System.Configuration.ConfigurationErrorsException("No entry '" + sectionName + "' in RemoteConfigurationManager"); } XmlDocument doc = new XmlDocument(); doc.Load(stream); XmlSerializerSectionHandler.GetConfigInstance(doc.DocumentElement, entry.EntryType); }
public static object CreateAndSetupWatcher(XmlNode section, string path, Type type, EventHandler OnConfigFileChangedByFile ) { object obj = XmlSerializerSectionHandler.GetConfigInstance(section, type); SetupWatcher(path, obj); if (OnConfigFileChangedByFile != null) { RegisterReloadNotification(path, OnConfigFileChangedByFile); } return(obj); }
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 = XmlSerializerSectionHandler.CreateAndSetupWatcher(doc.DocumentElement, path, type, OnConfigFileChanged); XmlSerializerSectionHandler.GetConfigVersion(doc.DocumentElement, out major, out minor); return(obj); } //相对目录 自动同步xml文件 string source = "SourceV2"; string fileName = Path.GetFileName(path); string mapPath = LocalConfigurationManager.MapConfigPath(""); if (mapPath.IndexOf(source) > 0) { var sourceDir = mapPath.Substring(0, mapPath.IndexOf(source) + source.Length); var sourceFilePath = Path.Combine(sourceDir, "MB.configs//" + fileName); if (File.Exists(sourceFilePath)) { File.Copy(sourceFilePath, path, true); if (File.Exists(path)) { XmlDocument doc = new XmlDocument(); doc.Load(path); object obj = XmlSerializerSectionHandler.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); }
protected override object OnCreate(string sectionName, Type type, out int major, out int minor) { string fileName = GetFileName(sectionName); string path = GetPath(sectionName); object obj = CreateLocalObject(type, path, out major, out minor); if (obj != null) { return(obj); } //Get Remote Config version major = XmlSerializerSectionHandler.GetConfigurationClassMajorVersion(type); minor = XmlSerializerSectionHandler.DefaultUninitMinorVersion; try { RemoteConfigSectionParam param = GetServerVersion(sectionName, major); if (param != null) { //download from remote! if (Download(param.SectionName, param.DownloadUrl, path, CheckDownloadStream)) { obj = CreateLocalObject(type, path, out major, out minor); } } } catch (Exception ex) { HandleException(ex, "Error when download configuration '" + sectionName + "' from remote server for the firet time", sectionName); } //if object is null use default object instead if (obj == null) { Log("Cannot get section '" + sectionName + "' with type '" + type.Name + "' from RemoteConfiguration, create empty instance instead"); obj = Activator.CreateInstance(type); XmlSerializerSectionHandler.SetupWatcher(path, obj); XmlSerializerSectionHandler.RegisterReloadNotification(path, OnConfigFileChanged); } return(obj); }
void OnConfigFileChanged(object sender, EventArgs args) { try { string filePath = ((FileChangedEventArgs)args).FileName; string sectionName = GetSectionName(filePath); XmlDocument doc = new XmlDocument(); doc.Load(filePath); int major, minor; XmlSerializerSectionHandler.GetConfigVersion(doc.DocumentElement, out major, out minor); ConfigEntry entry = GetEntry(sectionName); if (entry != null) { entry.MinorVersion = minor; } } catch (Exception ex) { logger.HandleException(ex, "RemoteConfigurationManager"); } }