Program.cs 1.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758
  1. using Microsoft.AspNetCore.Hosting;
  2. using Microsoft.Extensions.DependencyInjection;
  3. using Microsoft.Extensions.Hosting;
  4. using System;
  5. using System.Runtime.InteropServices;
  6. using System.Threading;
  7. using System.Threading.Tasks;
  8. namespace WCS.Service
  9. {
  10. public class Program
  11. {
  12. public static void Main(string[] args)
  13. {
  14. bool create = false;
  15. using (Mutex mt = new Mutex(true, "WCS", out create))
  16. {
  17. if (create)
  18. {
  19. CreateHostBuilder(args).Build().Run();
  20. }
  21. else
  22. {
  23. Console.WriteLine("请勿重复运行");
  24. Task.Delay(2000).Wait();
  25. }
  26. }
  27. }
  28. public static IHostBuilder CreateHostBuilder(string[] args)
  29. {
  30. //是否是win平台
  31. bool isWin = RuntimeInformation.IsOSPlatform(OSPlatform.Windows);
  32. Console.WriteLine($"win:{isWin}");
  33. if (isWin)
  34. {
  35. return Host.CreateDefaultBuilder(args)
  36. .UseWindowsService()//win
  37. .ConfigureWebHostDefaults(web =>
  38. {
  39. web.UseUrls("http://*:8080");
  40. web.UseStartup<Startup>();
  41. })
  42. .ConfigureServices((hostContext, services) =>
  43. {
  44. services.AddDbContext<WCSDB>();
  45. services.AddHostedService<Worker>();
  46. });
  47. }
  48. return Host.CreateDefaultBuilder(args)
  49. .UseSystemd()//linux
  50. .ConfigureServices((hostContext, services) =>
  51. {
  52. services.AddHostedService<Worker>();
  53. });
  54. }
  55. }
  56. }