Program.cs 2.3 KB

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