Program.cs 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  1. using System.Text.Json;
  2. using Base;
  3. using Common;
  4. using Extensions;
  5. using Filters;
  6. using Infrastructure;
  7. using Infrastructure.Model;
  8. using Microsoft.AspNetCore.Http.Features;
  9. using Microsoft.AspNetCore.Mvc;
  10. using Microsoft.AspNetCore.Server.Kestrel.Core;
  11. using Microsoft.Extensions.FileProviders;
  12. using Middleware;
  13. using Services;
  14. using SummerBoot.Core;
  15. using Util;
  16. var builder = WebApplication.CreateBuilder(args);
  17. // Add services to the container.
  18. builder.Services.AddControllers();
  19. // Learn more about configuring Swagger/OpenAPI at https://aka.ms/aspnetcore/swashbuckle
  20. builder.Services.AddEndpointsApiExplorer();
  21. //注入HttpContextAccessor
  22. builder.Services.AddSingleton<IHttpContextAccessor, HttpContextAccessor>();
  23. //IPRatelimit
  24. // builder.Services.AddIPRate(builder.Configuration);
  25. builder.Services.AddSession();
  26. builder.Services.AddHttpContextAccessor();
  27. builder.Services.AddCors(option => option.AddPolicy("cors", policy => policy.AllowAnyHeader().AllowAnyMethod().AllowCredentials().SetIsOriginAllowed(_ => true)));
  28. builder.Services.Configure<KestrelServerOptions>(x => x.AllowSynchronousIO = true).Configure<IISServerOptions>(x => x.AllowSynchronousIO = true);
  29. //绑定整个对象到Model上
  30. builder.Services.Configure<OptionsSetting>(builder.Configuration);
  31. // builder.Configuration.AddJsonFile("iprate.json");
  32. //配置文件
  33. builder.Services.AddSingleton(new AppSettings(builder.Configuration));
  34. //请求大小限制
  35. builder.Services.AddRequestLimit(builder.Configuration);
  36. //注册REDIS 服务
  37. var openRedis = builder.Configuration["RedisServer:open"];
  38. if (openRedis == "1")
  39. {
  40. RedisServer.Initalize();
  41. }
  42. //配置文件
  43. builder.Services.AddSingleton(new AppSettings(builder.Configuration));
  44. //app服务注册
  45. builder.Services.AddAppService();
  46. builder.Services.AddMvc(options =>
  47. {
  48. options.Filters.Add(typeof(GlobalActionMonitor));//全局注册
  49. options.Filters.Add(typeof(AuthorizationFilter));
  50. })
  51. .AddJsonOptions(options =>
  52. {
  53. //options.JsonSerializerOptions.NumberHandling = JsonNumberHandling.AllowReadingFromString | JsonNumberHandling.WriteAsString;
  54. options.JsonSerializerOptions.WriteIndented = true;
  55. options.JsonSerializerOptions.Converters.Add(new JsonConverterUtil.DateTimeConverter());
  56. options.JsonSerializerOptions.Converters.Add(new JsonConverterUtil.DateTimeNullConverter());
  57. options.JsonSerializerOptions.Converters.Add(new StringConverter());
  58. //PropertyNamingPolicy属性用于前端传过来的属性的格式策略,目前内置的仅有一种策略CamelCase
  59. options.JsonSerializerOptions.PropertyNamingPolicy = JsonNamingPolicy.CamelCase;
  60. //options.JsonSerializerOptions.PropertyNameCaseInsensitive = true;//属性可以忽略大小写格式,开启后性能会降低
  61. });
  62. builder.Services.AddSummerBoot();
  63. builder.Services.AddSummerBootFeign();
  64. var app = builder.Build();
  65. InternalApp.ServiceProvider = app.Services;
  66. InternalApp.Configuration = builder.Configuration;
  67. InternalApp.WebHostEnvironment = app.Environment;
  68. //初始化db
  69. builder.Services.AddDb(app.Environment);
  70. var workId = builder.Configuration["workId"].ParseToInt();
  71. if (app.Environment.IsDevelopment())
  72. {
  73. workId += 1;
  74. }
  75. SnowFlakeSingle.WorkId = workId;
  76. //使用全局异常中间件
  77. app.UseMiddleware<GlobalExceptionMiddleware>();
  78. app.Use(next => new RequestDelegate(
  79. async context =>
  80. {
  81. context.Request.EnableBuffering();
  82. await next(context);
  83. }
  84. ));
  85. // Configure the HTTP request pipeline.
  86. if (app.Environment.IsDevelopment())
  87. {
  88. }
  89. app.UseStaticFiles();
  90. app.UseCors();
  91. app.UseHttpsRedirection();
  92. app.UseAuthorization();
  93. app.UseRouting();
  94. app.UseEndpoints(endpoints =>
  95. {
  96. endpoints.MapControllerRoute(
  97. name: "default",
  98. pattern: "{controller=Home}/{action=Index}/{Id?}");
  99. });
  100. app.MapControllerRoute(
  101. name: "default",
  102. pattern: "{controller=Home}/{action=Index}/{id?}");
  103. app.MapControllers();
  104. app.Urls.Add("http://*:5800");
  105. // PrizeDo.sendPrize("{\"PosSn\":\"CS00000000004720\"}");
  106. app.Run();