Startup.cs 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  1. using System.Text;
  2. using System.IO;
  3. using Newtonsoft.Json.Serialization;
  4. using Microsoft.AspNetCore.Builder;
  5. using Microsoft.AspNetCore.Hosting;
  6. using Microsoft.Extensions.Configuration;
  7. using Microsoft.Extensions.DependencyInjection;
  8. using Microsoft.Extensions.FileProviders;
  9. using Microsoft.AspNetCore.DataProtection;
  10. using Microsoft.Extensions.Hosting;
  11. using Microsoft.OpenApi.Models;
  12. using YiSha.Util;
  13. using YiSha.Util.Model;
  14. using YiSha.Business.AutoJob;
  15. using YiSha.Admin.WebApi.Controllers;
  16. using YiSha.Business.AutoJob.IBusiness;
  17. using System;
  18. using Autofac;
  19. using YiSha.Admin.WebApi.AutoFac;
  20. using Autofac.Extensions.DependencyInjection;
  21. using YiSha.Data.Repository;
  22. namespace YiSha.Admin.WebApi
  23. {
  24. public class Startup
  25. {
  26. public IConfiguration Configuration { get; }
  27. public Startup(IConfiguration configuration, IWebHostEnvironment env)
  28. {
  29. Configuration = configuration;
  30. GlobalContext.LogWhenStart(env);
  31. GlobalContext.HostingEnvironment = env;
  32. }
  33. // This method gets called by the runtime. Use this method to add services to the container.
  34. public IServiceProvider ConfigureServices(IServiceCollection services)
  35. {
  36. services.AddSwaggerGen(config =>
  37. {
  38. config.SwaggerDoc("v1", new OpenApiInfo { Title = "YiSha Api", Version = "v1" });
  39. });
  40. services.AddOptions();
  41. services.AddCors();
  42. services.AddControllers(options =>
  43. {
  44. options.ModelMetadataDetailsProviders.Add(new ModelBindingMetadataProvider());
  45. }).AddNewtonsoftJson(options =>
  46. {
  47. // 返回数据首字母不小写,CamelCasePropertyNamesContractResolver是小写
  48. options.SerializerSettings.ContractResolver = new DefaultContractResolver();
  49. });
  50. services.AddMemoryCache();
  51. services.AddDataProtection().PersistKeysToFileSystem(new DirectoryInfo(GlobalContext.HostingEnvironment.ContentRootPath + Path.DirectorySeparatorChar + "DataProtection"));
  52. Encoding.RegisterProvider(CodePagesEncodingProvider.Instance); // 注册Encoding
  53. GlobalContext.SystemConfig = Configuration.GetSection("SystemConfig").Get<SystemConfig>();
  54. GlobalContext.Services = services;
  55. GlobalContext.Configuration = Configuration;
  56. return RegisterAutofac(services);//注册Autofac
  57. }
  58. // This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
  59. public void Configure(IApplicationBuilder app, IWebHostEnvironment env, IJobCenterBusiness jobCenterBusiness)
  60. {
  61. if (env.IsDevelopment())
  62. {
  63. GlobalContext.SystemConfig.Debug = true;
  64. app.UseDeveloperExceptionPage();
  65. }
  66. else
  67. {
  68. app.UseDeveloperExceptionPage();
  69. }
  70. string resource = Path.Combine(env.ContentRootPath, "Resource");
  71. FileHelper.CreateDirectory(resource);
  72. app.UseStaticFiles(new StaticFileOptions
  73. {
  74. OnPrepareResponse = GlobalContext.SetCacheControl
  75. });
  76. app.UseStaticFiles(new StaticFileOptions
  77. {
  78. RequestPath = "/Resource",
  79. FileProvider = new PhysicalFileProvider(resource),
  80. OnPrepareResponse = GlobalContext.SetCacheControl
  81. });
  82. app.UseMiddleware(typeof(GlobalExceptionMiddleware));
  83. app.UseCors(builder =>
  84. {
  85. builder.WithOrigins(GlobalContext.SystemConfig.AllowCorsSite.Split(',')).AllowAnyHeader().AllowAnyMethod().AllowCredentials();
  86. });
  87. app.UseSwagger(c =>
  88. {
  89. c.RouteTemplate = "api-doc/{documentName}/swagger.json";
  90. });
  91. app.UseSwaggerUI(c =>
  92. {
  93. c.RoutePrefix = "api-doc";
  94. c.SwaggerEndpoint("v1/swagger.json", "YiSha Api v1");
  95. });
  96. app.UseRouting();
  97. app.UseEndpoints(endpoints =>
  98. {
  99. endpoints.MapControllerRoute("default", "{controller=ApiHome}/{action=Index}/{id?}");
  100. });
  101. GlobalContext.ServiceProvider = app.ApplicationServices;
  102. if (!GlobalContext.SystemConfig.Debug)
  103. {
  104. jobCenterBusiness.Start(); // 定时任务
  105. }
  106. }
  107. /// <summary>
  108. /// Autofac容器注入
  109. /// </summary>
  110. /// <param name="services"></param>
  111. /// <returns></returns>
  112. private IServiceProvider RegisterAutofac(IServiceCollection services)
  113. {
  114. services.AddScoped<IRepositoryFactory, RepositoryFactory>();
  115. //实例化Autofac容器
  116. var builder = new ContainerBuilder();
  117. //将Services中的服务填充到Autofac中
  118. builder.Populate(services);
  119. //新模块组件注册
  120. builder.RegisterModule<AutofacModuleRegister>();
  121. //创建容器
  122. var Container = builder.Build();
  123. //第三方IOC接管 core内置DI容器
  124. return new AutofacServiceProvider(Container);
  125. }
  126. }
  127. }