using Autofac; using Autofac.Extensions.DependencyInjection; using Microsoft.AspNetCore.Builder; using Microsoft.AspNetCore.DataProtection; using Microsoft.AspNetCore.Hosting; using Microsoft.AspNetCore.Http; using Microsoft.Extensions.Configuration; using Microsoft.Extensions.DependencyInjection; using Microsoft.Extensions.FileProviders; using Microsoft.Extensions.Hosting; using Newtonsoft.Json.Serialization; using System; using System.IO; using System.Text; using System.Text.Encodings.Web; using System.Text.Unicode; using YiSha.Admin.Web.AutoFac; using YiSha.Admin.Web.Controllers; using YiSha.Data.Repository; using YiSha.Util; using YiSha.Util.Model; namespace YiSha.Admin.Web { public class Startup { public IConfiguration Configuration { get; } public IWebHostEnvironment WebHostEnvironment { get; set; } public Startup(IConfiguration configuration, IWebHostEnvironment env) { Configuration = configuration; WebHostEnvironment = env; 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) { if (WebHostEnvironment.IsDevelopment()) { services.AddRazorPages().AddRazorRuntimeCompilation(); } services.Configure(options => { // This lambda determines whether user consent for non-essential cookies is needed for a given request. options.CheckConsentNeeded = context => true; options.MinimumSameSitePolicy = SameSiteMode.None; }); services.AddSingleton(HtmlEncoder.Create(UnicodeRanges.All)); services.AddControllersWithViews(options => { options.Filters.Add(); options.ModelMetadataDetailsProviders.Add(new ModelBindingMetadataProvider()); }).AddNewtonsoftJson(options => { // 返回数据首字母不小写,CamelCasePropertyNamesContractResolver是小写 options.SerializerSettings.ContractResolver = new DefaultContractResolver(); }); services.AddMemoryCache(); services.AddSession(); services.AddHttpContextAccessor(); services.AddOptions(); 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) { if (!string.IsNullOrEmpty(GlobalContext.SystemConfig.VirtualDirectory)) { app.UsePathBase(new PathString(GlobalContext.SystemConfig.VirtualDirectory)); // 让 Pathbase 中间件成为第一个处理请求的中间件, 才能正确的模拟虚拟路径 } if (WebHostEnvironment.IsDevelopment()) { GlobalContext.SystemConfig.Debug = true; app.UseDeveloperExceptionPage(); } else { app.UseExceptionHandler("/Home/Error"); } 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 }); string areas = Path.Combine(env.ContentRootPath, "Areas"); FileHelper.CreateDirectory(areas); app.UseStaticFiles(new StaticFileOptions { OnPrepareResponse = GlobalContext.SetCacheControl }); app.UseStaticFiles(new StaticFileOptions { RequestPath = "/Areas", FileProvider = new PhysicalFileProvider(areas), OnPrepareResponse = GlobalContext.SetCacheControl }); app.UseSession(); app.UseRouting(); app.UseEndpoints(endpoints => { endpoints.MapControllerRoute("areas", "{area:exists}/{controller=Home}/{action=Index}/{id?}"); endpoints.MapControllerRoute("default", "{controller=Home}/{action=Index}/{id?}"); }); GlobalContext.ServiceProvider = app.ApplicationServices; } /// /// 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); } } }