Program.cs 1.8 KB

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