Startup.cs 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  1. using System;
  2. using System.Net.Http;
  3. using GxPress.Api.ServiceExtensions;
  4. using GxPress.Common.AppOptions;
  5. using GxPress.Common.Middleware;
  6. using GxPress.Common.Tools;
  7. using Microsoft.AspNetCore.Builder;
  8. using Microsoft.AspNetCore.Hosting;
  9. using Microsoft.AspNetCore.Http.Features;
  10. using Microsoft.AspNetCore.Mvc;
  11. using Microsoft.Extensions.Configuration;
  12. using Microsoft.Extensions.DependencyInjection;
  13. using Microsoft.Extensions.Hosting;
  14. using Newtonsoft.Json;
  15. using Newtonsoft.Json.Serialization;
  16. using Quartz;
  17. using Quartz.Impl;
  18. namespace GxPress.Api
  19. {
  20. public class Startup
  21. {
  22. public IConfiguration Configuration { get; }
  23. private IWebHostEnvironment currentEnvironment { get; set; }
  24. public Startup(IConfiguration configuration)
  25. {
  26. Configuration = configuration;
  27. ConfigHelper.Configs = Configuration;
  28. }
  29. // This method gets called by the runtime. Use this method to add services to the container.
  30. // For more information on how to configure your application, visit https://go.microsoft.com/fwlink/?LinkID=398940
  31. public void ConfigureServices(IServiceCollection services)
  32. {
  33. services.AddSingleton<ISchedulerFactory, StdSchedulerFactory>();
  34. services.AddControllers().AddNewtonsoftJson(options =>
  35. {
  36. options.SerializerSettings.ContractResolver = new DefaultContractResolver();
  37. //options.UseCamelCasing(true);
  38. options.SerializerSettings.DateFormatString = "yyyy-MM-dd HH:mm:ss";
  39. //// 忽略循环引用
  40. //options.SerializerSettings.ReferenceLoopHandling = ReferenceLoopHandling.Ignore;
  41. //// 如字段为null值,该字段不会返回到前端
  42. // options.SerializerSettings.NullValueHandling = NullValueHandling.Include;
  43. }).AddJsonOptions(options =>
  44. {
  45. options.JsonSerializerOptions.PropertyNamingPolicy = null;
  46. }).SetCompatibilityVersion(CompatibilityVersion.Version_3_0);
  47. services.AddHttpClient("yowkoblog", c => { c.BaseAddress = new Uri("https://lx.gxcbcmjt.com/"); })
  48. .ConfigurePrimaryHttpMessageHandler(h =>
  49. {
  50. var handler = new HttpClientHandler();
  51. if (currentEnvironment.IsDevelopment())
  52. {
  53. //handler.ServerCertificateCustomValidationCallback = delegate { return true; };
  54. handler.ServerCertificateCustomValidationCallback = HttpClientHandler.DangerousAcceptAnyServerCertificateValidator;
  55. }
  56. return handler;
  57. });
  58. //文件上传限制60MB
  59. services.Configure<FormOptions>(options =>
  60. {
  61. options.MultipartBodyLengthLimit = 60000000;
  62. });
  63. //数据库
  64. var databaseSection = Configuration.GetSection("Database");
  65. var cacheSection = Configuration.GetSection("Cache:ConnectionString");
  66. var storageOptions = Configuration.GetSection("Storage").Get<StorageOptions>();
  67. services.Configure<DatabaseOptions>(databaseSection);
  68. services.AddDistributedCache(cacheSection.Value);
  69. services.AddStorage(storageOptions);
  70. services.Configure<ApiBehaviorOptions>(o =>
  71. {
  72. o.InvalidModelStateResponseFactory = actionContext =>
  73. new BadRequestObjectResult(JsonConvert.SerializeObject(new
  74. {
  75. Message = "参数错误",
  76. StackTrace = JsonConvert.SerializeObject(actionContext.ModelState)
  77. }));
  78. });
  79. // var client = new CSRedisClient(redisSection.Value);
  80. // //初始化 RedisHelper
  81. // RedisHelper.Initialization(client);
  82. // //注册分布式缓存
  83. // var options = Options.Create(new Microsoft.Extensions.Caching.Redis.RedisCacheOptions
  84. // {
  85. // Configuration = redisSection.Value,
  86. // InstanceName = string.Empty,
  87. // });
  88. // services.AddSingleton<IDistributedCache>(new RedisCache(options));
  89. services.AddRepositories();
  90. services.AddServices();
  91. services.AddMappers();
  92. services.AddJwtAuthentication(Configuration);
  93. services.AddCors(options =>
  94. {
  95. options.AddPolicy(
  96. "AllowAny",
  97. x =>
  98. {
  99. x.AllowAnyHeader()
  100. .AllowAnyMethod()
  101. .SetIsOriginAllowed(_ => true)
  102. .AllowCredentials();
  103. });
  104. });
  105. services.AddSwashbuckle();
  106. }
  107. // This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
  108. public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
  109. {
  110. currentEnvironment=env;
  111. var handler = new HttpClientHandler();
  112. if (env.IsDevelopment())
  113. {
  114. handler.ServerCertificateCustomValidationCallback = delegate { return true; };
  115. app.UseDeveloperExceptionPage();
  116. }
  117. //最上层的自定义异常处理中间件
  118. app.UseMiddleware<ExceptionMiddleware>();
  119. app.UseRouting();
  120. app.UseJwtAuthorization();
  121. app.UseStaticFiles();
  122. app.UseCors("AllowAny");
  123. app.UseSwashbuckle();
  124. app.UseEndpoints(endpoints =>
  125. {
  126. endpoints.MapControllers();
  127. endpoints.MapControllerRoute("default", "{controller=Home}/{action=Index}/{id?}");
  128. });
  129. }
  130. }
  131. }