Program.cs 2.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  1. using Microsoft.AspNetCore.Hosting;
  2. using System.Runtime.InteropServices;
  3. using WCS.Core;
  4. using WCS.WebApi;
  5. using RedisHelper = WCS.Core.Redis.RedisHelper;
  6. namespace WCS.Service
  7. {
  8. public class Program
  9. {
  10. /// <summary>
  11. /// 程序入口
  12. /// </summary>
  13. /// <param name="args"></param>
  14. public static void Main(string[] args)
  15. {
  16. #region 接入Redis
  17. RedisHelper.CreateContext(AppSettings.Config.GetConnectionString("Redis"), "default", true);
  18. #endregion 接入Redis
  19. //互斥锁检测
  20. var mutexName = RedisHelper.Default.Get("Mutex");
  21. if (string.IsNullOrEmpty(mutexName))
  22. {
  23. RedisHelper.Default.Set("Mutex", " ");
  24. Console.WriteLine("请在Redis中配置互斥量值");
  25. return;
  26. }
  27. using var mt = new Mutex(true, mutexName);
  28. if (mt.WaitOne())
  29. {
  30. Configs.AddSystemMode(SystemMode.虚拟plc);
  31. CreateHostBuilder(args).Build().Run(); mt.ReleaseMutex();
  32. }
  33. else
  34. {
  35. Console.WriteLine("请勿重复运行");
  36. //InfoLog.INFO_INIT("请勿重复运行");
  37. Task.Delay(2000).Wait();
  38. }
  39. }
  40. /// <summary>
  41. /// 创建一个主机构建器
  42. /// </summary>
  43. /// <param name="args"></param>
  44. /// <returns></returns>
  45. public static IHostBuilder CreateHostBuilder(string[] args)
  46. {
  47. //是否是win平台
  48. var isWin = RuntimeInformation.IsOSPlatform(OSPlatform.Windows);
  49. Console.WriteLine($"win:{isWin}");
  50. if (isWin)
  51. {
  52. return Host.CreateDefaultBuilder(args)
  53. .UseWindowsService()//win
  54. .ConfigureWebHostDefaults(web => //网络访问配置
  55. {
  56. web.UseUrls("http://*:8080"); //设备访问端口
  57. web.UseStartup<Startup>(); //调用启动服务
  58. })
  59. .ConfigureServices((_, services) =>
  60. {
  61. //services.AddDbContext<WCSDB>();
  62. services.AddHostedService<Worker>();
  63. services.AddHttpClient();
  64. });
  65. }
  66. return Host.CreateDefaultBuilder(args)
  67. .UseSystemd()//linux
  68. .ConfigureServices((_, services) =>
  69. {
  70. services.AddHostedService<Worker>();
  71. });
  72. }
  73. }
  74. }