public static void UnInstallAutoStartService() { SystemMessageNotify.ShowTip("SiMay远程控制被控服务正在卸载服务!"); if (ServiceInstallerHelper.UnInstallService(AppConfiguartion.ServiceName)) { Environment.Exit(0); } else { SystemMessageNotify.ShowTip("SiMay远程控制被控服务卸载失败!"); LogHelper.DebugWriteLog("Service UnInstall Not Completed!!"); } }
private void ActivateApplicationService(TcpSocketSaeaSession session) { var activateServicePack = GetMessageEntity <ActivateServicePack>(session); string key = activateServicePack.ApplicationKey; //获取当前消息发送源主控端标识 long accessId = this.GetAccessId(session); var context = SysUtil.ControlTypes.FirstOrDefault(x => x.ServiceKey.Equals(key)); if (context != null) { var serviceName = context.AppServiceType.GetCustomAttribute <ServiceNameAttribute>(true); SystemMessageNotify.ShowTip($"正在进行远程操作:{(serviceName.IsNull() ? context.ServiceKey : serviceName.Name) }"); var appService = Activator.CreateInstance(context.AppServiceType, null) as ApplicationRemoteService; appService.AppServiceKey = context.ServiceKey; appService.AccessId = accessId; this.PostTaskToQueue(appService); } }
public static void InstallAutoStartService() { SystemMessageNotify.ShowTip("SiMay远程控制被控服务正在安装服务!"); var svcFullName = Assembly.GetExecutingAssembly().Location; var parameter = " \"-serviceStart\"";//服务启动标志 svcFullName += parameter; if (ServiceInstallerHelper.InstallService(svcFullName, AppConfiguartion.ServiceName, AppConfiguartion.ServiceDisplayName)) { SystemMessageNotify.ShowTip("SiMay远程控制被控服务安装完成!"); //服务安装完成启动成功 Environment.Exit(0); } else { SystemMessageNotify.ShowTip("SiMay远程控制被控服务安装失败!"); LogHelper.DebugWriteLog("Service Install Not Completed!!"); } }
static void Main(string[] args) { if (args.Any(c => c.Equals(SERVICE_START, StringComparison.OrdinalIgnoreCase))) { ServiceBase.Run(new ServiceBase[] { new Service() }); } else//非服务启动 { var startParameter = new StartParameterEx() { Host = "94.191.115.121", //Port = 520, Port = 522, GroupName = "默认分组", RemarkInformation = "SiMayService", IsHide = false, IsMutex = false, IsAutoStart = false, //SessionMode = 0, SessionMode = 1, AccessKey = 5200, ServiceVersion = "正式6.0", RunTimeText = DateTime.Now.ToString(), UniqueId = "AAAAAAAAAAAAAAA11111111", ServiceName = "SiMayService", ServiceDisplayName = "SiMay远程被控服务", InstallService = false }; try { byte[] binary = File.ReadAllBytes(Application.ExecutablePath); var sign = BitConverter.ToInt16(binary, binary.Length - sizeof(Int16)); if (sign == 9999) { var length = BitConverter.ToInt32(binary, binary.Length - sizeof(Int16) - sizeof(Int32)); byte[] bytes = new byte[length]; Array.Copy(binary, binary.Length - sizeof(Int16) - sizeof(Int32) - length, bytes, 0, length); var options = PacketSerializeHelper.DeserializePacket <ServiceOptions>(bytes); startParameter.Host = options.Host; startParameter.Port = options.Port; startParameter.RemarkInformation = options.Remark; startParameter.IsAutoStart = options.IsAutoRun; startParameter.IsHide = options.IsHide; startParameter.AccessKey = options.AccessKey; startParameter.SessionMode = options.SessionMode; startParameter.UniqueId = options.Id + $"_{Environment.MachineName}"; startParameter.IsMutex = options.IsMutex; startParameter.GroupName = options.GroupName; startParameter.InstallService = options.InstallService; startParameter.ServiceName = options.ServiceName; startParameter.ServiceDisplayName = options.ServiceDisplayName; } } catch { } if (startParameter.IsMutex) { //进程互斥体 bool bExist; Mutex mutex = new Mutex(true, startParameter.UniqueId + "_SiMayService", out bExist); if (!bExist) { Environment.Exit(0); } } AppConfiguartion.HasSystemAuthority = args.Any(c => c.Equals(SERVICE_USER_START, StringComparison.OrdinalIgnoreCase)); AppConfiguartion.ServiceName = startParameter.ServiceName.IsNullOrEmpty() ? "SiMayService" : startParameter.ServiceName; AppConfiguartion.ServiceDisplayName = startParameter.ServiceDisplayName.IsNullOrEmpty() ? "SiMay远程被控服务" : startParameter.ServiceDisplayName; //初始化连接服务 if (args.Any(c => c.Equals(SERVICE_USER_START, StringComparison.OrdinalIgnoreCase))) { LogHelper.DebugWriteLog("初始化连接服务"); new UserTrunkContext(args); } //非SYSTEM用户进程启动则进入安装服务 if (startParameter.InstallService && !args.Any(c => c.Equals(SERVICE_USER_START, StringComparison.OrdinalIgnoreCase))) { SystemSessionHelper.InstallAutoStartService(); } Application.SetUnhandledExceptionMode(UnhandledExceptionMode.CatchException); AppDomain.CurrentDomain.UnhandledException += CurrentDomain_UnhandledException; Application.ThreadException += Application_ThreadException; try { new MainService.MainService(startParameter); } catch (Exception ex) { WriteException("main service exception!", ex); } SystemMessageNotify.InitializeNotifyIcon(); SystemMessageNotify.ShowTip("SiMay远程控制被控服务已启动!"); Application.Run(); } }