123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143 |
- 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<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, 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(); // 定时任务
- }
- }
- /// <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);
- }
- }
- }
|