Program.cs 2.4 KB

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