public override void Close() { base.Close(); ServerInfo.Exit(); StopServer(); // Thread.Sleep(2000); #if NO if (SignalR != null) { SignalR.Dispose(); SignalR = null; } #endif }
static void Main(string[] args) { System.AppDomain.CurrentDomain.UnhandledException += UnhandledExceptionTrapper; ServiceShortName = "dp2RouterService"; // 修改配置 if (args.Length == 1 && args[0].Equals("setting")) { InitialConfig(); Console.WriteLine("(直接回车表示不修改当前值)"); Console.WriteLine("请输入数据目录: (当前值为 " + DataDir + ")"); string strValue = Console.ReadLine(); if (string.IsNullOrEmpty(strValue) == false) { DataDir = strValue; } SaveConfig(); InitialConfig(); // 设置需要保存到数据目录 config.xml 中的其他参数 ServerInfo.Initial(DataDir, true); Console.WriteLine("请输入服务器端口号: (当前值为 " + ServerInfo.ServerPort + ")"); strValue = Console.ReadLine(); if (string.IsNullOrEmpty(strValue) == false) { ServerInfo.ServerPort = strValue; } Console.WriteLine("请输入 dp2MServer URL: (当前值为 " + ServerInfo.Url + ")"); strValue = Console.ReadLine(); if (string.IsNullOrEmpty(strValue) == false) { ServerInfo.Url = strValue; } Console.WriteLine("请输入 dp2MServer 用户名: (当前值为 " + ServerInfo.UserName + ")"); strValue = Console.ReadLine(); if (string.IsNullOrEmpty(strValue) == false) { ServerInfo.UserName = strValue; } Console.WriteLine("请输入 dp2MServer 密码:"); Console.BackgroundColor = Console.ForegroundColor; strValue = Console.ReadLine(); Console.ResetColor(); if (string.IsNullOrEmpty(strValue) == false) { ServerInfo.Password = strValue; } ServerInfo.SaveCfg(DataDir); ServerInfo.Exit(); Console.WriteLine(); Console.WriteLine("注:修改将在服务重启以后生效"); Console.WriteLine("(按回车键返回)"); Console.ReadLine(); return; } if (args.Length == 1 && (args[0].Equals("install") || args[0].Equals("uninstall")) ) { bool bInstall = true; if (args[0].Equals("uninstall")) { bInstall = false; } // 注册为 Windows Service // string strExePath = System.Reflection.Assembly.GetExecutingAssembly().GetName().CodeBase; string strExePath = Assembly.GetExecutingAssembly().Location; Console.WriteLine((bInstall ? "注册" : "注销") + " Windows Service ..."); string strError = ""; int nRet = ServiceUtil.InstallService(strExePath, bInstall, out strError); if (nRet == -1) { Console.WriteLine("error: " + strError); } Console.WriteLine(); Console.WriteLine("(按回车键返回)"); Console.ReadLine(); return; } if (args.Length == 1 && args[0].Equals("console")) { if (Initial() == false) { return; } new Program().ConsoleRun(); } else { // 这是从命令行启动的情况 if (Environment.UserInteractive == true) { Console.WriteLine("dp2Router 用法:"); Console.WriteLine("注册 Windows Service: dp2router install"); Console.WriteLine("注销 Windows Service: dp2router uninstall"); Console.WriteLine("以控制台方式运行: dp2router console"); Console.WriteLine("修改配置参数: dp2router setting"); Console.WriteLine("(按回车键返回)"); Console.ReadLine(); return; } if (Initial() == false) { return; } // 这是被当作 service 启动的情况 ServiceBase.Run(new Program()); } }