public bool Execute() { bool successful = true; var tasks = container.ResolveAll <BootstrapperTask>().OrderBy(t => t.Order).ToList(); foreach (var task in tasks) { YmatouLoggingService.Debug("YmatouFramework.Bootstrapper 开始执行 '{0}' ({1})", task.GetType().FullName, task.Description); try { if (task.Execute() == TaskContinuation.Break) { YmatouLoggingService.Error("YmatouFramework.Bootstrapper 执行中断 '{0}' ({1})", task.GetType().FullName, task.Description); successful = false; break; } } catch (Exception ex) { successful = false; YmatouLoggingService.Error("YmatouFramework.Bootstrapper 执行出错 '{0}',异常信息:{1}", task.GetType().FullName, ex.ToString()); } } ; return(successful); }
public static void Start(bool lazyStart = false) { if (lazyStart) { LazyStart = true; YmatouLoggingService.Debug("延迟启动..."); return; } Status = YmatouFrameworkStatus.Starting; var watch = Stopwatch.StartNew(); YmatouLoggingService.Debug("YmatouFramework开始启动...内部版本号:{0}", Version); bootstrapper = new Bootstrapper(container); if (bootstrapper.Execute()) { Status = YmatouFrameworkStatus.Started; YmatouLoggingService.Debug("YmatouFramework启动完成!,耗时:{0} 秒...内部版本号:{1}", watch.ElapsedMilliseconds / 1000.0, Version); } else { Status = YmatouFrameworkStatus.FailedToStart; throw new Exception(string.Format("YmatouFramework启动失败!,耗时 {0} 秒 {1}", watch.ElapsedMilliseconds / 1000.0, Version)); } }
public Bootstrapper(IUnityContainer container) { this.container = container; //container.RegisterInstance<IUnityContainer>(container); YmatouLoggingService.Debug("load BootstrapperTask..."); BuildManagerWrapper.Current.ConcreteTypes .Where(type => typeof(BootstrapperTask).IsAssignableFrom(type)) .Each(type => container.RegisterMultipleTypesAsSingleton(typeof(BootstrapperTask), type)); YmatouLoggingService.Debug("load BootstrapperTask end..."); }
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); YmatouLoggingService.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) { YmatouLoggingService.Debug("反序列化异常,类型名称:{0},异常信息:{1}", typeof(T).Name, ex.ToString()); return(defVal); } } } }
public static void AssertArgumentNotNull(object object1, string message) { if (!ycfg.EnableAssert) { return; } if (object1 == null) { YmatouLoggingService.Error("{0} {1}", object1, message); throw new InvalidOperationException(message); } }
public static void Stop() { Status = YmatouFrameworkStatus.Ending; YmatouLoggingService.Debug("YmatouFramework开始清理"); var watch = Stopwatch.StartNew(); bootstrapper.Dispose(); Status = YmatouFrameworkStatus.Ended; YmatouLoggingService.Debug("YmatouFramework清理完成!耗时 :{0}秒 ", watch.ElapsedMilliseconds / 1000.0); }
protected override void InternalDispose() { container.ResolveAll <BootstrapperTask>().OrderByDescending(t => t.Order).Each(task => { try { YmatouLoggingService.Debug("YmatouFramework.Bootstrapper 开始清理 '{0}' ({1})", task.GetType().FullName, task.Description); task.Dispose(); } catch (Exception ex) { YmatouLoggingService.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); YmatouLoggingService.Error(errMsg); return(false); } }
private static T GetInstance <T>(string name = "") { try { if (string.IsNullOrEmpty(name)) { return(YmatouFramework.Container.Resolve <T>()); } else { return(YmatouFramework.Container.Resolve <T>(name)); } } catch (Exception ex) { YmatouLoggingService.Error("YmatouFramework.LocalServiceLocator 不能解析 '{0}',异常信息:{1}", typeof(T).FullName, ex.ToString()); throw; } }
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) { YmatouLoggingService.Error("序列化异常,类型名称:{0},异常信息:{1}", typeof(T).Name, ex.ToString()); } } } }