123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154 |
- 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<CookiePolicyOptions>(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<GlobalExceptionFilter>();
- 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<SystemConfig>();
- 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;
- }
- /// <summary>
- /// Autofac容器注入
- /// </summary>
- /// <param name="services"></param>
- /// <returns></returns>
- private IServiceProvider RegisterAutofac(IServiceCollection services)
- {
- services.AddScoped<IRepositoryFactory, RepositoryFactory>();
- //实例化Autofac容器
- var builder = new ContainerBuilder();
- //将Services中的服务填充到Autofac中
- builder.Populate(services);
- //新模块组件注册
- builder.RegisterModule<AutofacModuleRegister>();
- //创建容器
- var Container = builder.Build();
- //第三方IOC接管 core内置DI容器
- return new AutofacServiceProvider(Container);
- }
- }
- }
|