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.
 
 
 

105 lines
4.6 KiB

  1. using System;
  2. using System.Threading.Tasks;
  3. using Microsoft.Extensions.DependencyInjection;
  4. using Microsoft.Extensions.Hosting;
  5. using Microsoft.Extensions.Logging;
  6. using JT808.Protocol;
  7. using Microsoft.Extensions.Configuration;
  8. using NLog.Extensions.Logging;
  9. using JT808.Gateway.NormalHosting.Impl;
  10. using JT808.Gateway.MsgLogging;
  11. using JT808.Gateway.Transmit;
  12. using JT808.Gateway.NormalHosting.Services;
  13. using JT808.Gateway.Abstractions;
  14. using JT808.Gateway.SessionNotice;
  15. using JT808.Gateway.Client;
  16. using JT808.Gateway.NormalHosting.Jobs;
  17. using JT808.Gateway.WebApiClientTool;
  18. using Microsoft.AspNetCore.Builder;
  19. using Microsoft.AspNetCore.Routing;
  20. using Microsoft.AspNetCore.Server;
  21. using Microsoft.AspNetCore.Hosting;
  22. using Microsoft.Extensions.Configuration.Json;
  23. using JT808.Gateway.Abstractions.Configurations;
  24. using System.Net;
  25. using JT808.Gateway.Extensions;
  26. using JT808.Gateway.NormalHosting.Customs;
  27. namespace JT808.Gateway.NormalHosting
  28. {
  29. class Program
  30. {
  31. static void Main(string[] args)
  32. {
  33. var builder = WebApplication.CreateBuilder();
  34. builder.Host.ConfigureAppConfiguration((hostingContext, config) =>
  35. {
  36. config.SetBasePath(AppDomain.CurrentDomain.BaseDirectory)
  37. .AddJsonFile("appsettings.json", optional: false, reloadOnChange: true)
  38. .AddJsonFile($"appsettings.{ hostingContext.HostingEnvironment.EnvironmentName}.json", optional: true, reloadOnChange: true);
  39. })
  40. .ConfigureLogging((context, logging) =>
  41. {
  42. Console.WriteLine($"Environment.OSVersion.Platform:{Environment.OSVersion.Platform.ToString()}");
  43. NLog.LogManager.LoadConfiguration($"Configs/nlog.{Environment.OSVersion.Platform.ToString()}.config");
  44. logging.AddNLog(new NLogProviderOptions { CaptureMessageTemplates = true, CaptureMessageProperties = true });
  45. logging.SetMinimumLevel(LogLevel.Trace);
  46. })
  47. .ConfigureServices((hostContext, services) =>
  48. {
  49. //使用内存队列实现会话通知
  50. services.AddSingleton<JT808SessionService>();
  51. services.AddSingleton<IJT808SessionProducer, JT808SessionProducer>();
  52. services.AddSingleton<IJT808SessionConsumer, JT808SessionConsumer>();
  53. //使用内存队列实现应答生产消费
  54. services.AddSingleton<JT808MsgReplyDataService>();
  55. services.AddSingleton<IJT808MsgReplyProducer, JT808MsgReplyProducer>();
  56. services.AddJT808Configure()
  57. //添加客户端工具
  58. .AddClient()
  59. .Builder()
  60. //方式1:客户端webapi调用
  61. .AddWebApiClientTool<JT808HttpClientExt>(hostContext.Configuration)
  62. .AddGateway(hostContext.Configuration)
  63. .AddMessageHandler<JT808CustomMessageHandlerImpl>()
  64. .AddMsgReplyConsumer<JT808MsgReplyConsumer>()
  65. .AddMsgLogging<JT808MsgLogging>()
  66. .AddSessionNotice()
  67. .AddTransmit(hostContext.Configuration)
  68. .AddTcp()
  69. .AddUdp();
  70. //方式2:客户端webapi调用
  71. //services.AddJT808WebApiClientTool(hostContext.Configuration);
  72. //httpclient客户端调用
  73. services.AddHostedService<CallHttpClientJob>();
  74. //客户端测试 依赖AddClient()服务
  75. //services.AddHostedService<UpJob>();
  76. //需要跨域的
  77. services.AddCors(options =>
  78. options.AddPolicy("jt808", builder =>
  79. builder.AllowAnyMethod()
  80. .AllowAnyHeader()
  81. .AllowCredentials()
  82. .SetIsOriginAllowed(o => true)));
  83. });
  84. builder.WebHost.UseKestrel((app, serverOptions) =>
  85. {
  86. //1.配置webapi端口监听
  87. var jT808Configuration = app.Configuration.GetSection(nameof(JT808Configuration)).Get<JT808Configuration>();
  88. serverOptions.ListenAnyIP(jT808Configuration.WebApiPort);
  89. })
  90. .ConfigureServices((hostContext, services) =>
  91. {
  92. services.AddControllers();
  93. });
  94. var app = builder.Build();
  95. app.UseCors();
  96. app.MapControllers().RequireCors("jt808");
  97. app.Run();
  98. }
  99. }
  100. }