Program.cs 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  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, "由于异常而停止程序!");
  32. }
  33. }
  34. finally
  35. {
  36. // Ensure to flush and stop internal timers/threads before application-exit (Avoid segmentation fault on Linux)
  37. NLog.LogManager.Shutdown();
  38. }
  39. }
  40. public static IHostBuilder CreateHostBuilder(string[] args) =>
  41. Host.CreateDefaultBuilder(args)
  42. .ConfigureWebHostDefaults(webBuilder =>
  43. {
  44. #region Kestrel
  45. webBuilder.ConfigureKestrel(options =>
  46. {
  47. //监听端口 5001 用于 Web Service
  48. options.ListenAnyIP(8089, configure => configure.Protocols = HttpProtocols.Http1);
  49. //监听端口 5001 用于 Web Service
  50. //options.ListenAnyIP(5201, configure => configure.Protocols = HttpProtocols.Http2);
  51. });
  52. #endregion
  53. webBuilder.UseStartup<Startup>()
  54. .ConfigureLogging(logging =>
  55. {
  56. logging.ClearProviders();
  57. logging.SetMinimumLevel(LogLevel.Trace);
  58. })
  59. .UseNLog(); // NLog: Setup NLog for Dependency injection;
  60. })
  61. .ConfigureServices((hostContext, services) =>
  62. {
  63. services.AddHostedService<Worker>();
  64. })
  65. .UseWindowsService()
  66. ;
  67. }
  68. }