using AutoMapper; using LangGuan.Command.AutoMapper; using LangGuan.Command.Extension; using LangGuan.Configuration; using LangGuan.Middleware; using LangGuan.Services; using LangGuan.Services.Job; using Microsoft.AspNetCore.Builder; using Microsoft.AspNetCore.Hosting; using Microsoft.AspNetCore.Mvc; using Microsoft.AspNetCore.Mvc.ApiExplorer; using Microsoft.Extensions.Configuration; using Microsoft.Extensions.DependencyInjection; using Microsoft.Extensions.DependencyInjection.Extensions; using Microsoft.Extensions.Hosting; using Microsoft.Extensions.Logging; using Microsoft.Extensions.Options; using Microsoft.OpenApi.Models; using Quartz; using Quartz.Impl; using Quartz.Spi; using Swashbuckle.AspNetCore.SwaggerGen; using System; using System.Collections.Generic; using System.Linq; using System.Text.Json; using System.Threading.Tasks; namespace LangGuan { public class Startup { public Startup(IConfiguration configuration) { Configuration = configuration; } public IConfiguration Configuration { get; } // This method gets called by the runtime. Use this method to add services to the container. public void ConfigureServices(IServiceCollection services) { services.AddControllers(options => { options.Filters.Add(); }).AddNewtonsoftJson(option => { //option.SerializerSettings.ContractResolver = new Newtonsoft.Json.Serialization.DefaultContractResolver(); //option.SerializerSettings.DateFormatString = "yyyy-MM-dd HH:mm:ss"; }).AddJsonOptions(option => { //userName option.JsonSerializerOptions.PropertyNamingPolicy = JsonNamingPolicy.CamelCase; }); services.AddMemoryCache(); //请求地址转成小写 services.AddRouting(options => options.LowercaseUrls = true); #region 使用AutoMapper var mapperConfig = new MapperConfiguration(mc => { mc.AddProfile(new CustomProfile()); }); IMapper mapper = mapperConfig.CreateMapper(); services.AddSingleton(mapper); #endregion //cors services.AddCors(options => { options.AddPolicy("cors", builder => { builder.AllowAnyOrigin(); builder.AllowAnyMethod(); builder.AllowAnyHeader(); }); }); //版本号 #region API versioning services.AddApiVersioning(options => { options.ReportApiVersions = true; options.DefaultApiVersion = new ApiVersion(1, 0); }); services.AddVersionedApiExplorer(options => { options.GroupNameFormat = "'v'VVV"; options.SubstituteApiVersionInUrl = true; }); #endregion //Swagger 配置 services.AddTransient, SwaggerConfigureOptions>(); services.AddSwaggerGen(options => { options.IncludeXmlComments(System.IO.Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "LangGuan.xml")); }); #region 服务注入 services.AddTransient(); services.AddTransient(); services.AddTransient(); services.AddTransient(); services.AddTransient(); services.AddTransient(); services.AddTransient(); services.AddTransient(); //定时任务 services.AddTransient(); services.AddTransient(); services.AddTransient(); services.AddTransient(); services.AddSingleton(); services.AddJobService(); #endregion } // This method gets called by the runtime. Use this method to configure the HTTP request pipeline. public void Configure(IApplicationBuilder app, IWebHostEnvironment env, IApiVersionDescriptionProvider apiVersionDescriptionProvider) { if (env.IsDevelopment()) { app.UseDeveloperExceptionPage(); } app.UseStaticFiles(); app.UseRouting(); app.UseCors("cors"); app.UseAuthorization(); app.UseEndpoints(endpoints => { endpoints.MapControllers(); }); app.UseSwagger(options => { options.PreSerializeFilters.Add((swagger, req) => { swagger.Servers = new List() { new OpenApiServer() { Url = $"http://{req.Host}" } }; }); }); app.UseSwaggerUI(options => { foreach (var desc in apiVersionDescriptionProvider.ApiVersionDescriptions) { //配置swagger.json options.SwaggerEndpoint($"/swagger/{desc.GroupName}/swagger.json", desc.ApiVersion.ToString()); options.DefaultModelsExpandDepth(-1); options.DocExpansion(Swashbuckle.AspNetCore.SwaggerUI.DocExpansion.None); //配置swagger访问首页 options.RoutePrefix = "api/LangGuan"; } }); } } }