Example #1
0
        /// <summary>
        /// 启动主服务
        /// </summary>
        /// <param name="service"></param>
        private void ServiceExecute(string service)
        {
            IServiceFactory sf = ReflectionOptimizer.CreateInstanceMethod(service)() as IServiceFactory;

            if (sf != null)
            {
                sf.ServiceInit();
            }
        }
Example #2
0
 /// <summary>
 /// 启动扩展服务
 /// </summary>
 /// <param name="extensions"></param>
 private void ExtensionExecute(IEnumerable <string> extensions)
 {
     foreach (string extension in extensions)
     {
         IExtension ex = ReflectionOptimizer.CreateInstanceMethod(extension)() as IExtension;
         if (ex != null)
         {
             ex.Execute();
         }
     }
 }
        public object ServiceInit()
        {
            string configPath = string.Format("{0}/Config/ConsoleCommand.xml", AppDomain.CurrentDomain.BaseDirectory);

            if (!File.Exists(configPath))
            {
                return(null);
            }
            var root = XElement.Load(configPath);
            var cmds = from data in root.Elements("Command") select data;

            foreach (var cmd in cmds)
            {
                IConsoleCommand consoleCmd = ReflectionOptimizer.CreateInstanceMethod((string)cmd.Attribute("Type"))() as IConsoleCommand;
                if (consoleCmd != null)
                {
                    string key = (string)cmd.Attribute("Key");
                    consoleCmd.SetKey(key);
                    if (!_cmds.ContainsKey(key))
                    {
                        _cmds.Add(key, consoleCmd);
                    }
                }
            }
            bool flag = true;

            while (flag)
            {
                var readStr = Console.ReadLine().Split(_split, StringSplitOptions.RemoveEmptyEntries);
                if (readStr.Length > 0)
                {
                    if (_cmds.ContainsKey(readStr[0]))
                    {
                        IConsoleCommand consoleCmd = _cmds[readStr[0]];
                        if (!consoleCmd.IsStarting)
                        {
                            ThreadPool.QueueUserWorkItem(state =>
                            {
                                try
                                {
                                    Console.WriteLine("{0} {1}", DateTime.Now, consoleCmd.StartLine);
                                    consoleCmd.Fun(readStr);
                                    Console.WriteLine("{0} {1}", DateTime.Now, consoleCmd.CompleteLine);
                                }
                                catch (Exception ex)
                                {
                                    Console.WriteLine(ex);
                                }
                                finally
                                {
                                    consoleCmd.IsStarting = false;
                                }
                            });
                            consoleCmd.IsStarting = true;
                        }
                        else if (consoleCmd.IsStarting)
                        {
                            Console.WriteLine(consoleCmd.RunningLine);
                        }
                    }
                    else if (readStr[0] == "exit")
                    {
                        flag = false;
                    }
                    else
                    {
                        Console.WriteLine("Bad Command,Please Input Again!");
                    }
                }
                else
                {
                    Console.WriteLine("Bad Command,Please Input Again!");
                }
            }
            return(null);
        }