Example #1
0
        /// <summary>
        /// 重启指定服务(可根据服务名称在加载的DLL中查询启动)
        /// </summary>
        /// <param name="ServiceName"></param>
        /// <param name="Ass"></param>
        /// <returns></returns>
        public static bool RestartService(Assembly AssServiceObj)
        {
            bool bResult = false;

            try
            {
                if (AssServiceObj.GetTypes().Count() > 0)
                {
                    foreach (var v in AssServiceObj.GetTypes())
                    {
                        if (v.IsNested || !v.IsPublic)
                        {
                            continue;
                        }
                        #region 1、判断服务是否存在并且Open
                        if (IsServiceRun(v.FullName))
                        {
                            ServiceHost sh = (ServiceHost)ServiceSuccessTable[v.FullName];
                            sh.Close();
                        }
                        #endregion
                        #region 2、判断服务是否在服务集合中,如果在则Remove
                        if (ServiceSuccessTable.ContainsKey(v.FullName.Trim()))
                        {
                            ServiceSuccessTable.Remove(v.FullName.Trim());
                        }
                        #endregion
                        #region 3、启动并配置WCF
                        Type type = AssServiceObj.GetType(v.FullName, true);
                        if (type.IsClass)
                        {
                            ServiceHost sh = new ServiceHost(type);

                            sh.Open();
                            ServiceSuccessTable.Add(v.FullName, sh);
                            bResult = true;
                            Console.ForegroundColor = ConsoleColor.Green;
                            Console.Write("\t【Load】");
                            Console.ForegroundColor = ConsoleColor.White;
                            Console.WriteLine("启动服务:" + v.FullName);
                        }
                        #endregion
                    }
                }
            }
            catch (Exception ex)
            {
                Console.ForegroundColor = ConsoleColor.Red;
                Console.Write("\t【Fail】");
                Console.ForegroundColor = ConsoleColor.White;
                Console.WriteLine("启动服务:" + AssServiceObj.FullName);
                Console.ForegroundColor = ConsoleColor.Red;
                Console.WriteLine("\t【Error】" + ex.Message);
                bResult = false;
            }

            return(bResult);
        }
Example #2
0
        /// <summary>
        /// 重启所有已加载的服务
        /// </summary>
        /// <returns></returns>
        public static bool ResetAllService()
        {
            bool bResult = false;

            if (AssSuccessTable != null && AssSuccessTable.Count > 0)
            {
                #region 1、关闭所有已启动成功的服务
                if (ServiceSuccessTable != null && ServiceSuccessTable.Count > 0)
                {
                    foreach (DictionaryEntry v in ServiceSuccessTable)
                    {
                        ServiceHost sh = (ServiceHost)v.Value;
                        if (sh.State == CommunicationState.Opened)
                        {
                            sh.Close();
                            Console.ForegroundColor = ConsoleColor.Blue;
                            Console.Write("\t【Done】");
                            Console.ForegroundColor = ConsoleColor.White;
                            Console.WriteLine("关闭服务:" + v.Key);
                        }
                    }
                }

                ServiceSuccessTable.Clear();
                #endregion

                #region 2、启动所有已载入的数据
                foreach (DictionaryEntry de in AssSuccessTable)
                {
                    Console.ForegroundColor = ConsoleColor.Yellow;
                    Console.Write("【Load】");
                    Console.ForegroundColor = ConsoleColor.White;
                    Console.WriteLine("启动服务集:" + de.Key.ToString());

                    if (RestartService((Assembly)de.Value))
                    {
                        Console.ForegroundColor = ConsoleColor.Green;
                        Console.Write("【Done】");
                    }
                    else
                    {
                        Console.ForegroundColor = ConsoleColor.Red;
                        Console.Write("【Fail】");
                    }
                    Console.ForegroundColor = ConsoleColor.White;
                    Console.WriteLine("启动服务集:" + de.Key.ToString());
                }
                #endregion
            }
            else
            {
                throw new Exception("AssSuccessTable is empty");
            }
            return(bResult);
        }
Example #3
0
        /// <summary>
        /// 根据服务名称获取服务状态
        /// </summary>
        /// <param name="ServiceName">服务名称</param>
        /// <returns>true:运行;false:停止</returns>
        public static bool IsServiceRun(string ServiceName)
        {
            bool bResult = false;

            if (!string.IsNullOrWhiteSpace(ServiceName))
            {
                if (ServiceSuccessTable != null &&
                    ServiceSuccessTable.ContainsKey(ServiceName.Trim()))
                {
                    ServiceHost sh = (ServiceHost)ServiceSuccessTable[ServiceName.Trim()];
                    if (sh.State == CommunicationState.Opened)  // 服务已启动
                    {
                        bResult = true;
                    }
                }
            }
            return(bResult);
        }
Example #4
0
        /// <summary>
        /// 根据指定的服务名称卸载服务
        /// </summary>
        /// <param name="ServiceName">服务名称</param>
        /// <returns>true:卸载成功;false:卸载失败</returns>
        public static bool UnInstallService(string ServiceName)
        {
            bool bResult = false;

            if (ServiceSuccessTable != null &&
                ServiceSuccessTable.ContainsKey(ServiceName.Trim()))
            {
                try
                {
                    ServiceHost sh = (ServiceHost)ServiceSuccessTable[ServiceName.Trim()];
                    if (sh.State == CommunicationState.Opened)  // 服务已启动
                    {
                        sh.Close();
                    }
                    ServiceSuccessTable.Remove(ServiceName.Trim());
                    Console.ForegroundColor = ConsoleColor.Green;
                    Console.Write("【Done】");
                    Console.ForegroundColor = ConsoleColor.White;
                    Console.WriteLine("卸载服务:" + ServiceName.Trim());
                    bResult = true;
                }
                catch (Exception ex)
                {
                    Console.ForegroundColor = ConsoleColor.Red;
                    Console.Write("【Fail】");
                    Console.ForegroundColor = ConsoleColor.White;
                    Console.WriteLine("卸载服务:" + ServiceName.Trim());
                    Console.ForegroundColor = ConsoleColor.Red;
                    Console.WriteLine("\t【Error】" + ex.Message);
                    bResult = false;
                }
            }
            else
            {
                Console.ForegroundColor = ConsoleColor.Red;
                Console.Write("【Fail】");
                Console.ForegroundColor = ConsoleColor.White;
                Console.WriteLine("卸载服务:" + ServiceName.Trim());
                Console.ForegroundColor = ConsoleColor.Red;
                Console.WriteLine("【Error】:此服务不存在!");
                bResult = false;
            }
            return(bResult);
        }