Exemple #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("AdhesiveFramework.Bootstrapper 开始执行 '{0}' ({1})", task.GetType().FullName, task.Description);
                try
                {
                    if (task.Execute() == TaskContinuation.Break)
                    {
                        LocalLoggingService.Warning("AdhesiveFramework.Bootstrapper 执行中断 '{0}' ({1})", task.GetType().FullName, task.Description);
                        successful = false;
                        break;
                    }
                }
                catch (Exception ex)
                {
                    LocalLoggingService.Error("AdhesiveFramework.Bootstrapper 执行出错 '{0}',异常信息:{1}", task.GetType().FullName, ex.ToString());
                }
            }
            ;
            return(successful);
        }
Exemple #2
0
 public static void End()
 {
     Status = AdhesiveFrameworkStatus.Ending;
     LocalLoggingService.Info("AdhesiveFramework开始清理...");
     bootstrapper.Dispose();
     Status = AdhesiveFrameworkStatus.Ended;
     LocalLoggingService.Info("AdhesiveFramework清理完成!");
     LocalLoggingService.Close();
 }
Exemple #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);
                    }
                }
            }
        }
Exemple #4
0
 protected override void InternalDispose()
 {
     container.ResolveAll <BootstrapperTask>().OrderByDescending(t => t.Order).Each(task =>
     {
         try
         {
             LocalLoggingService.Debug("AdhesiveFramework.Bootstrapper 开始清理 '{0}' ({1})", task.GetType().FullName, task.Description);
             task.Dispose();
         }
         catch (Exception ex)
         {
             LocalLoggingService.Error("AdhesiveFramework.Bootstrapper 清理出错 '{0}',异常信息:{1}", task.GetType().FullName, ex.ToString());
         }
     });
 }
Exemple #5
0
 public static void Start()
 {
     Status = AdhesiveFrameworkStatus.Starting;
     LocalLoggingService.Info("AdhesiveFramework开始启动...版本号:{0}", Version);
     bootstrapper = new Bootstrapper(container);
     if (bootstrapper.Execute())
     {
         Status = AdhesiveFrameworkStatus.Started;
         LocalLoggingService.Info("AdhesiveFramework启动完成!...版本号:{0}", Version);
     }
     else
     {
         Status = AdhesiveFrameworkStatus.FailedToStart;
         throw new Exception("AdhesiveFramework启动失败!");
     }
 }
Exemple #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);
     }
 }
Exemple #7
0
 public static T GetService <T>()
 {
     if (AdhesiveFramework.Status == AdhesiveFrameworkStatus.NotStarted)
     {
         throw new Exception("AdhesiveFramework尚未启动!");
     }
     else
     {
         try
         {
             T instance = AdhesiveFramework.Container.Resolve <T>();
             return(instance);
         }
         catch (Exception ex)
         {
             LocalLoggingService.Error("AdhesiveFramework.LocalServiceLocator 不能解析 '{0}',异常信息:{1}", typeof(T).FullName, ex.ToString());
             throw;
         }
     }
 }
Exemple #8
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());
                    }
                }
            }
        }