You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 

92 lines
3.4 KiB

  1. using JT808.Gateway.Abstractions.Enums;
  2. using JT808.Gateway.ReplyMessage;
  3. using JT808.Gateway.MsgLogging;
  4. using JT808.Gateway.SessionNotice;
  5. using JT808.Protocol;
  6. using Microsoft.Extensions.Configuration;
  7. using Microsoft.Extensions.DependencyInjection;
  8. using Microsoft.Extensions.DependencyInjection.Extensions;
  9. using Microsoft.Extensions.Hosting;
  10. using Microsoft.Extensions.Logging;
  11. using System;
  12. using System.Threading.Tasks;
  13. using JT808.Gateway.SimpleServer.Impl;
  14. using JT808.Gateway.SimpleServer.Services;
  15. using JT808.Gateway.Abstractions;
  16. using JT808.Gateway.Transmit;
  17. using Microsoft.AspNetCore.Hosting;
  18. using JT808.Gateway.Abstractions.Configurations;
  19. using Microsoft.AspNetCore.Builder;
  20. namespace JT808.Gateway.SimpleServer
  21. {
  22. class Program
  23. {
  24. static async Task Main(string[] args)
  25. {
  26. var serverHostBuilder = new HostBuilder()
  27. .ConfigureAppConfiguration((hostingContext, config) =>
  28. {
  29. config.SetBasePath(AppDomain.CurrentDomain.BaseDirectory);
  30. config.AddJsonFile("appsettings.json", optional: false, reloadOnChange: true);
  31. })
  32. .ConfigureLogging((context, logging) =>
  33. {
  34. logging.AddConsole();
  35. })
  36. .ConfigureServices((hostContext, services) =>
  37. {
  38. services.AddSingleton<ILoggerFactory, LoggerFactory>();
  39. services.AddSingleton(typeof(ILogger<>), typeof(Logger<>));
  40. //使用内存队列实现会话通知
  41. services.AddSingleton<JT808SessionService>();
  42. services.AddSingleton<IJT808SessionProducer, JT808SessionProducer>();
  43. services.AddSingleton<IJT808SessionConsumer, JT808SessionConsumer>();
  44. services.AddJT808Configure()
  45. .AddGateway(hostContext.Configuration)
  46. .AddMessageHandler<JT808MessageHandlerImpl>()
  47. .AddMsgLogging<JT808MsgLogging>()
  48. .AddSessionNotice()
  49. .AddTransmit(hostContext.Configuration)
  50. .AddTcp()
  51. .AddUdp()
  52. .Builder();
  53. }).ConfigureWebHostDefaults(webBuilder => {
  54. webBuilder.UseKestrel((app, ksOptions) =>
  55. {
  56. //1.配置webapi端口监听
  57. var jT808Configuration = app.Configuration.GetSection(nameof(JT808Configuration)).Get<JT808Configuration>();
  58. ksOptions.ListenAnyIP(jT808Configuration.WebApiPort);
  59. })
  60. .UseStartup<Startup>();
  61. });
  62. await serverHostBuilder.RunConsoleAsync();
  63. }
  64. }
  65. public class Startup
  66. {
  67. public Startup(IConfiguration configuration)
  68. {
  69. Configuration = configuration;
  70. }
  71. public IConfiguration Configuration { get; }
  72. public void ConfigureServices(IServiceCollection services)
  73. {
  74. services.AddControllers();
  75. }
  76. public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
  77. {
  78. app.UseRouting();
  79. app.UseEndpoints(endpoints =>
  80. {
  81. endpoints.MapControllers();
  82. });
  83. }
  84. }
  85. }