using System.Text.Json; using Base; using Common; using Extensions; using Filters; using Infrastructure; using Infrastructure.Model; using Microsoft.AspNetCore.Mvc; using Microsoft.AspNetCore.Server.Kestrel.Core; using Middleware; using Services; using Util; var builder = WebApplication.CreateBuilder(args); // Add services to the container. builder.Services.AddControllers(); // Learn more about configuring Swagger/OpenAPI at https://aka.ms/aspnetcore/swashbuckle builder.Services.AddEndpointsApiExplorer(); //注入HttpContextAccessor builder.Services.AddSingleton(); //IPRatelimit // builder.Services.AddIPRate(builder.Configuration); builder.Services.AddSession(); builder.Services.AddHttpContextAccessor(); builder.Services.Configure(x => x.AllowSynchronousIO = true).Configure(x => x.AllowSynchronousIO = true); //绑定整个对象到Model上 builder.Services.Configure(builder.Configuration); // builder.Configuration.AddJsonFile("iprate.json"); //配置文件 builder.Services.AddSingleton(new AppSettings(builder.Configuration)); //请求大小限制 builder.Services.AddRequestLimit(builder.Configuration); //注册REDIS 服务 var openRedis = builder.Configuration["RedisServer:open"]; if (openRedis == "1") { RedisServer.Initalize(); } //配置文件 builder.Services.AddSingleton(new AppSettings(builder.Configuration)); //app服务注册 builder.Services.AddAppService(); builder.Services.AddMvc(options => { options.Filters.Add(typeof(GlobalActionMonitor));//全局注册 options.Filters.Add(typeof(AuthorizationFilter)); }) .AddJsonOptions(options => { //options.JsonSerializerOptions.NumberHandling = JsonNumberHandling.AllowReadingFromString | JsonNumberHandling.WriteAsString; options.JsonSerializerOptions.WriteIndented = true; options.JsonSerializerOptions.Converters.Add(new JsonConverterUtil.DateTimeConverter()); options.JsonSerializerOptions.Converters.Add(new JsonConverterUtil.DateTimeNullConverter()); options.JsonSerializerOptions.Converters.Add(new StringConverter()); //PropertyNamingPolicy属性用于前端传过来的属性的格式策略,目前内置的仅有一种策略CamelCase options.JsonSerializerOptions.PropertyNamingPolicy = JsonNamingPolicy.CamelCase; //options.JsonSerializerOptions.PropertyNameCaseInsensitive = true;//属性可以忽略大小写格式,开启后性能会降低 }); var app = builder.Build(); InternalApp.ServiceProvider = app.Services; InternalApp.Configuration = builder.Configuration; InternalApp.WebHostEnvironment = app.Environment; //初始化db builder.Services.AddDb(app.Environment); var workId = builder.Configuration["workId"].ParseToInt(); if (app.Environment.IsDevelopment()) { workId += 1; } SnowFlakeSingle.WorkId = workId; //使用全局异常中间件 app.UseMiddleware(); app.Use(next => new RequestDelegate( async context => { context.Request.EnableBuffering(); await next(context); } )); // Configure the HTTP request pipeline. if (app.Environment.IsDevelopment()) { } app.UseHttpsRedirection(); app.UseAuthorization(); app.MapControllerRoute( name: "default", pattern: "{controller=Home}/{action=Index}/{id?}"); app.MapControllers(); app.Run();