public bool Execute() { bool successful = true; var tasks = container.ResolveAll <BootstrapperTask>().OrderBy(t => t.Order).ToList(); foreach (var task in tasks) { LocalLoggingService.Debug("YmatouFramework.Bootstrapper 开始执行 '{0}' ({1})", task.GetType().FullName, task.Description); try { if (task.Execute() == TaskContinuation.Break) { LocalLoggingService.Warning("YmatouFramework.Bootstrapper 执行中断 '{0}' ({1})", task.GetType().FullName, task.Description); successful = false; break; } } catch (Exception ex) { successful = false; LocalLoggingService.Error("YmatouFramework.Bootstrapper 执行出错 '{0}',异常信息:{1}", task.GetType().FullName, ex.ToString()); } } ; return(successful); }
public static T GetConfig <T>(string fileName, T defVal) { object instance = null; string fileFullName = GetConfigFileFullName(fileName); if (ConfigCache.TryGetValue(fileFullName, out instance)) { return((T)instance); } lock (Locker) { if (ConfigCache.TryGetValue(fileFullName, out instance)) { return((T)instance); } if (!File.Exists(fileFullName)) { TryCreateConfig(fileName, defVal); return(defVal); } XmlDocument doc = new XmlDocument(); try { doc.Load(fileFullName); } catch (Exception ex) { string errMsg = string.Format("加载配置文件 {0} 失败!使用默认配置文件!,异常信息:{1}", fileFullName, ex); LocalLoggingService.Error(errMsg); return(defVal); } ConfigFileWatcher configFileWatcher = null; if (!ConfigFileWatcherCache.TryGetValue(fileFullName, out configFileWatcher)) { ConfigFileWatcherCache.Add(fileFullName, new ConfigFileWatcher(fileFullName, OnConfigChanged)); } XmlSerializer xmlSerializer = new XmlSerializer(typeof(T)); using (StringReader sr = new StringReader(doc.OuterXml)) { try { instance = (T)xmlSerializer.Deserialize(sr); ConfigCache.Add(fileFullName, instance); return((T)instance); } catch (Exception ex) { LocalLoggingService.Debug("反序列化异常,类型名称:{0},异常信息:{1}", typeof(T).Name, ex.ToString()); return(defVal); } } } }
private static T GetInstance <T>() { try { T instance = YmatouFramework.Container.Resolve <T>(); return(instance); } catch (Exception ex) { LocalLoggingService.Error("YmatouFramework.LocalServiceLocator 不能解析 '{0}',异常信息:{1}", typeof(T).FullName, ex.ToString()); throw; } }
protected override void InternalDispose() { container.ResolveAll <BootstrapperTask>().OrderByDescending(t => t.Order).Each(task => { try { LocalLoggingService.Debug("YmatouFramework.Bootstrapper 开始清理 '{0}' ({1})", task.GetType().FullName, task.Description); task.Dispose(); } catch (Exception ex) { LocalLoggingService.Error("YmatouFramework.Bootstrapper 清理出错 '{0}',异常信息:{1}", task.GetType().FullName, ex.ToString()); } }); }
private static bool EnsureConfigDirectoryExists() { if (Directory.Exists(LocalConfigDirectory)) { return(true); } try { Directory.CreateDirectory(LocalConfigDirectory); return(true); } catch (Exception ex) { string errMsg = string.Format("创建目录 {0} 失败!异常信息:{1}", LocalConfigDirectory, ex); LocalLoggingService.Error(errMsg); return(false); } }
private static void TryCreateConfig <T>(string fileName, T defVal) { if (!EnsureConfigDirectoryExists()) { return; } string fileFullName = GetConfigFileFullName(fileName); if (File.Exists(fileFullName)) { return; } if (defVal == null) { return; } XmlWriterSettings settings = new XmlWriterSettings(); settings.Encoding = Encoding.UTF8; settings.Indent = true; XmlSerializer xs = new XmlSerializer(typeof(T), (string)null); using (FileStream fs = new FileStream(fileFullName, FileMode.Create)) { using (XmlWriter xw = XmlWriter.Create(fs, settings)) { try { xs.Serialize(xw, defVal); } catch (Exception ex) { LocalLoggingService.Error("序列化异常,类型名称:{0},异常信息:{1}", typeof(T).Name, ex.ToString()); } } } }