예제 #1
0
        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);
        }
예제 #2
0
        public static void Stop()
        {
            Status = YmatouFrameworkStatus.Ending;

            LocalLoggingService.Info("YmatouFramework开始清理...内部测试版本号:{0}", Version);

            bootstrapper.Dispose();
            Status = YmatouFrameworkStatus.Ended;

            LocalLoggingService.Info("YmatouFramework清理完成!...内部测试版本号:{0}", Version);
            LocalLoggingService.Close();
        }
예제 #3
0
        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);
                    }
                }
            }
        }
예제 #4
0
 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;
     }
 }
예제 #5
0
 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());
         }
     });
 }
예제 #6
0
 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);
     }
 }
예제 #7
0
        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());
                    }
                }
            }
        }
예제 #8
0
        public static void Start(bool lazyStart = false)
        {
            if (lazyStart)
            {
                LazyStart = true;
                LocalLoggingService.Debug("延迟启动...");
                return;
            }
            Status = YmatouFrameworkStatus.Starting;

            LocalLoggingService.Info("YmatouFramework开始启动...内部测试版本号:{0}", Version);

            bootstrapper = new Bootstrapper(container);
            if (bootstrapper.Execute())
            {
                Status = YmatouFrameworkStatus.Started;
                LocalLoggingService.Info("YmatouFramework启动完成!...内部测试版本号:{0}", Version);
            }
            else
            {
                Status = YmatouFrameworkStatus.FailedToStart;
                throw new Exception(string.Format("YmatouFramework启动失败! {0} {1}", Version, ""));
            }
        }