Program.cs 2.4 KB

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