Program.cs 2.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  1. using IMCS.CCS.Common;
  2. using Microsoft.AspNetCore.Hosting;
  3. using Microsoft.AspNetCore.Server.Kestrel.Core;
  4. using Microsoft.Extensions.DependencyInjection;
  5. using Microsoft.Extensions.Hosting;
  6. using Microsoft.Extensions.Logging;
  7. using NLog.Web;
  8. using System;
  9. using System.Threading.Tasks;
  10. namespace IMCS.CCS
  11. {
  12. public class Program
  13. {
  14. public static async Task Main(string[] args)
  15. {
  16. var logger = NLogBuilder.ConfigureNLog("nlog.config").GetCurrentClassLogger();
  17. var host = CreateHostBuilder(args).Build();
  18. try
  19. {
  20. //设置NLog
  21. LogUtil.Init(logger);
  22. logger.Debug("初始化 Main !");
  23. Console.WriteLine("初始化 Main !");
  24. await host.RunAsync();
  25. //host.Run();
  26. }
  27. catch (Exception exception)
  28. {
  29. if (!(exception is MessageBox))
  30. {
  31. //NLog: catch setup errors
  32. logger.Error(exception, "由于异常而停止程序!");
  33. }
  34. }
  35. finally
  36. {
  37. // Ensure to flush and stop internal timers/threads before application-exit (Avoid segmentation fault on Linux)
  38. NLog.LogManager.Shutdown();
  39. }
  40. }
  41. public static IHostBuilder CreateHostBuilder(string[] args) =>
  42. Host.CreateDefaultBuilder(args)
  43. .ConfigureWebHostDefaults(webBuilder =>
  44. {
  45. #region Kestrel
  46. webBuilder.ConfigureKestrel(options =>
  47. {
  48. //监听端口 5001 用于 Web Service
  49. options.ListenAnyIP(8089, configure => configure.Protocols = HttpProtocols.Http1);
  50. Console.WriteLine("初始化监听成功");
  51. //监听端口 5001 用于 Web Service
  52. //options.ListenAnyIP(5201, configure => configure.Protocols = HttpProtocols.Http2);
  53. });
  54. #endregion
  55. webBuilder.UseStartup<Startup>()
  56. .ConfigureLogging(logging =>
  57. {
  58. logging.ClearProviders();
  59. logging.SetMinimumLevel(LogLevel.Trace);
  60. })
  61. .UseNLog(); // NLog: Setup NLog for Dependency injection;
  62. })
  63. .ConfigureServices((hostContext, services) =>
  64. {
  65. services.AddHostedService<Worker>();
  66. })
  67. .UseWindowsService()
  68. ;
  69. }
  70. }