Program.cs 2.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  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. await host.RunAsync();
  24. //host.Run();
  25. }
  26. catch (Exception exception)
  27. {
  28. if (!(exception is MessageBox))
  29. {
  30. //NLog: catch setup errors
  31. logger.Error(exception, exception.Message);
  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. //监听端口 5001 用于 Web Service
  51. //options.ListenAnyIP(5201, configure => configure.Protocols = HttpProtocols.Http2);
  52. });
  53. #endregion
  54. webBuilder.UseStartup<Startup>()
  55. .ConfigureLogging(logging =>
  56. {
  57. logging.ClearProviders();
  58. logging.SetMinimumLevel(LogLevel.Trace);
  59. })
  60. .UseNLog(); // NLog: Setup NLog for Dependency injection;
  61. })
  62. .ConfigureServices((hostContext, services) =>
  63. {
  64. services.AddHostedService<Worker>();
  65. })
  66. .UseWindowsService()
  67. ;
  68. }
  69. }