using System.Text; using System.IO; using Newtonsoft.Json.Serialization; using Microsoft.AspNetCore.Builder; using Microsoft.AspNetCore.Hosting; using Microsoft.Extensions.Configuration; using Microsoft.Extensions.DependencyInjection; using Microsoft.Extensions.FileProviders; using Microsoft.AspNetCore.DataProtection; using Microsoft.Extensions.Hosting; using Microsoft.OpenApi.Models; using YiSha.Util; using YiSha.Util.Model; using YiSha.Business.AutoJob; using YiSha.Admin.WebApi.Controllers; using YiSha.Business.AutoJob.IBusiness; using System; using Autofac; using YiSha.Admin.WebApi.AutoFac; using Autofac.Extensions.DependencyInjection; using YiSha.Data.Repository; namespace YiSha.Admin.WebApi { public class Startup { public IConfiguration Configuration { get; } public Startup(IConfiguration configuration, IWebHostEnvironment env) { Configuration = configuration; GlobalContext.LogWhenStart(env); GlobalContext.HostingEnvironment = env; } // This method gets called by the runtime. Use this method to add services to the container. public IServiceProvider ConfigureServices(IServiceCollection services) { services.AddSwaggerGen(config => { config.SwaggerDoc("v1", new OpenApiInfo { Title = "YiSha Api", Version = "v1" }); }); services.AddOptions(); services.AddCors(); services.AddControllers(options => { options.ModelMetadataDetailsProviders.Add(new ModelBindingMetadataProvider()); }).AddNewtonsoftJson(options => { // 返回数据首字母不小写,CamelCasePropertyNamesContractResolver是小写 options.SerializerSettings.ContractResolver = new DefaultContractResolver(); }); services.AddMemoryCache(); services.AddDataProtection().PersistKeysToFileSystem(new DirectoryInfo(GlobalContext.HostingEnvironment.ContentRootPath + Path.DirectorySeparatorChar + "DataProtection")); Encoding.RegisterProvider(CodePagesEncodingProvider.Instance); // 注册Encoding GlobalContext.SystemConfig = Configuration.GetSection("SystemConfig").Get(); GlobalContext.Services = services; GlobalContext.Configuration = Configuration; return RegisterAutofac(services);//注册Autofac } // This method gets called by the runtime. Use this method to configure the HTTP request pipeline. public void Configure(IApplicationBuilder app, IWebHostEnvironment env, IJobCenterBusiness jobCenterBusiness) { if (env.IsDevelopment()) { GlobalContext.SystemConfig.Debug = true; app.UseDeveloperExceptionPage(); } else { app.UseDeveloperExceptionPage(); } string resource = Path.Combine(env.ContentRootPath, "Resource"); FileHelper.CreateDirectory(resource); app.UseStaticFiles(new StaticFileOptions { OnPrepareResponse = GlobalContext.SetCacheControl }); app.UseStaticFiles(new StaticFileOptions { RequestPath = "/Resource", FileProvider = new PhysicalFileProvider(resource), OnPrepareResponse = GlobalContext.SetCacheControl }); app.UseMiddleware(typeof(GlobalExceptionMiddleware)); app.UseCors(builder => { builder.WithOrigins(GlobalContext.SystemConfig.AllowCorsSite.Split(',')).AllowAnyHeader().AllowAnyMethod().AllowCredentials(); }); app.UseSwagger(c => { c.RouteTemplate = "api-doc/{documentName}/swagger.json"; }); app.UseSwaggerUI(c => { c.RoutePrefix = "api-doc"; c.SwaggerEndpoint("v1/swagger.json", "YiSha Api v1"); }); app.UseRouting(); app.UseEndpoints(endpoints => { endpoints.MapControllerRoute("default", "{controller=ApiHome}/{action=Index}/{id?}"); }); GlobalContext.ServiceProvider = app.ApplicationServices; if (!GlobalContext.SystemConfig.Debug) { jobCenterBusiness.Start(); // 定时任务 } } /// /// Autofac容器注入 /// /// /// private IServiceProvider RegisterAutofac(IServiceCollection services) { services.AddScoped(); //实例化Autofac容器 var builder = new ContainerBuilder(); //将Services中的服务填充到Autofac中 builder.Populate(services); //新模块组件注册 builder.RegisterModule(); //创建容器 var Container = builder.Build(); //第三方IOC接管 core内置DI容器 return new AutofacServiceProvider(Container); } } }