Program.cs 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192
  1. 
  2. using log4net;
  3. using SCADA.CommonLib;
  4. using SCADA.CommonLib.Helper;
  5. using SCADA.CommonLib.Service;
  6. using System;
  7. using System.Runtime.InteropServices;
  8. using System.Threading;
  9. using Topshelf;
  10. namespace SCADA_DAQ.Service
  11. {
  12. class ProgramMain
  13. {
  14. static void Main(string[] args)
  15. {
  16. ILog log = SCADA.CommonLib.LoggerHelper.Logger.CreateLogger(typeof(ProgramMain));
  17. AppDomain.CurrentDomain.UnhandledException += (s, e) =>
  18. {
  19. log.Error(e);
  20. };
  21. DisableConsoleCloseAction();
  22. Env.ParameterInit(Runtime.Instance);
  23. HostFactory.Run(x =>
  24. {
  25. var cfg = x.Service<Runtime>(
  26. s =>
  27. {
  28. s.ConstructUsing(name => Runtime.Instance);
  29. s.WhenStarted(t => t.Start());
  30. s.WhenPaused(t => t.Paused());
  31. s.WhenContinued(t => t.Resume());
  32. s.WhenStopped(t => t.Stop());
  33. });
  34. //x.UseLog4Net("log4net.config");
  35. x.SetDescription("SCADA 采集");
  36. x.SetDisplayName($"SCADA 采集 {ApplicationHelper.GetAppVersion()}");
  37. x.SetInstanceName("SCADA_Service");
  38. x.StartAutomatically(); // Automatic (Delayed) -- only available on .NET 4.0 or later
  39. x.RunAsLocalSystem();
  40. x.EnableServiceRecovery((e) =>
  41. {
  42. e.RestartService(0);//第一次失败执行
  43. e.RestartService(0);//第二次失败执行
  44. e.RestartService(0);//后续失败执行
  45. e.OnCrashOnly();
  46. e.SetResetPeriod(1);
  47. });
  48. x.OnException(ex =>
  49. {
  50. log.Error(ex); //异常处理
  51. })
  52. ;
  53. });
  54. }
  55. [DllImport("user32.dll")]
  56. private static extern IntPtr FindWindow(string lpClassName, string lpWindowName);
  57. [DllImport("user32.dll")]
  58. private static extern IntPtr GetSystemMenu(IntPtr hWnd, IntPtr bRevert);
  59. [DllImport("user32.dll")]
  60. private static extern IntPtr RemoveMenu(IntPtr hMenu, uint uPosition, uint uFlags);
  61. private static void DisableConsoleCloseAction()
  62. {
  63. bool flag = true;
  64. while (flag)
  65. {
  66. Console.Title = _ConsoleTitle;
  67. IntPtr hWnd = FindWindow(null, _ConsoleTitle);
  68. IntPtr systemMenu = GetSystemMenu(hWnd, IntPtr.Zero);
  69. uint uPosition = 61536U;
  70. if (RemoveMenu(systemMenu, uPosition, 0U).ToInt32() != 0)
  71. {
  72. Console.WriteLine("success to disable close button.");
  73. flag = false;
  74. }
  75. else
  76. {
  77. Console.WriteLine("try to disable close button...");
  78. Thread.Sleep(1000);
  79. }
  80. }
  81. }
  82. private const string _ConsoleTitle = "采集服务正在运行,请不要关闭此窗口!(PLEASE Don't Close This Window!)";
  83. }
  84. public class Runtime : IApp
  85. {
  86. private string _sessionid;
  87. private Runtime()
  88. {
  89. }
  90. private static Runtime _instance;
  91. /// <summary>
  92. ///
  93. /// </summary>
  94. public static Runtime Instance { get => _instance ?? (_instance = new Runtime()); private set { _instance = value; } }
  95. public ThemeConfig ThemeConfig { get; }
  96. public SystemConfig SystemConfig { get; }
  97. public RuntimeInfo RuntimeInfo { get; set; }
  98. public IServiceHost ServiceHost { get; set; }
  99. public void Start()
  100. {
  101. RuntimeInfo.ProductId = "";
  102. RuntimeInfo.SessionId = _sessionid ?? (_sessionid = Guid.NewGuid().ToString());
  103. Console.WriteLine($"App Start {DateTime.Now:yyyy-MM-dd HH:mm:ss.fff}");
  104. if (string.IsNullOrEmpty(SystemConfig.WebServer))
  105. {
  106. SystemConfig.WebServer = "www.imaodou.com.cn";
  107. }
  108. if (string.IsNullOrEmpty(SystemConfig.ProductTitle))
  109. {
  110. SystemConfig.ProductTitle = RuntimeInfo.ProductId;
  111. }
  112. if (string.IsNullOrEmpty(SystemConfig.AuthorInfo))
  113. {
  114. SystemConfig.AuthorInfo = "";
  115. }
  116. if (string.IsNullOrEmpty(SystemConfig.DefaultUser))
  117. {
  118. SystemConfig.DefaultUser = "Operator";
  119. }
  120. var host = SCADA_DAQ.ServiceHost.Instance;
  121. host.Start(this);
  122. ServiceHost = host;
  123. }
  124. public void Stop()
  125. {
  126. ServiceHost.Stop();
  127. }
  128. public void Paused()
  129. {
  130. }
  131. public void ReLoadLicense()
  132. {
  133. }
  134. public void Resume()
  135. {
  136. }
  137. public void Restart(bool runAsAdmin)
  138. {
  139. }
  140. public void Restart()
  141. {
  142. }
  143. public void ShowLongToast(object message, ControlStyle toastType = ControlStyle.Default)
  144. {
  145. }
  146. public void ShowShortToast(object message, ControlStyle toastType = ControlStyle.Default)
  147. {
  148. }
  149. public void ShowToast(object message, ControlStyle toastType = ControlStyle.Default, int showTime = 2000)
  150. {
  151. }
  152. }
  153. }