using log4net; using SuperSocket.SocketBase; using SuperSocket.SocketBase.Config; using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Diagnostics; using System.Linq; using System.ServiceProcess; using System.Text; using POSV.Service.Utils; using POSV.WindowsService.Logging; namespace POSV.WindowsService { public partial class MessagingServer : ServiceBase { private static NLog.Logger logger = NLog.LogManager.GetCurrentClassLogger(); public MessagingServer() { InitializeComponent(); } private Dictionary ParseArgs(string[] args) { Dictionary ret = new Dictionary(); foreach (string arg in args) { var split = arg.Split('='); if (split != null && split[0] != null && split[1] != null && split[0] != string.Empty) { ret.Add(split[0], split[1]); } } return ret; } protected override void OnStart(string[] args) { try { Dictionary argsDic = ParseArgs(args); string argsEventLogString = string.Empty; foreach (KeyValuePair arg in argsDic) { argsEventLogString += string.Format("{0}: {1}", arg.Key, arg.Value); } if (argsEventLogString != string.Empty) { argsEventLogString = string.Format("OnStart Arguments:\n{0}", argsEventLogString); logger.Info(argsEventLogString); } this.StartServer(); } catch(Exception ex) { logger.Error(ex, "服务中心启动异常"); } } protected override void OnStop() { this.StopServer(); } internal void DebugStartServer() { this.StartServer(); } internal void DebugStopServer() { this.StopServer(); } private void StartServer() { WSE.Instance.Start(); //启动MQTT服务 MqttBrokerUtils.Instance.Startup(); //启动Web服务 NancyUtils.Instance.Startup(); logger.Info("服务器启动成功!"); MqttUtils.Instance.Startup(); } private void StopServer() { MqttUtils.Instance.Stop(); WSE.Instance.Stop(); MqttBrokerUtils.Instance.Stop(); NancyUtils.Instance.Stop(); } protected override void OnContinue() { StartServer(); } protected override void OnPause() { StopServer(); } protected override void OnShutdown() { StopServer(); } } }