Program.cs 3.4 KB

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