Startup.cs 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  1. using Admin.NET.Core;
  2. using Furion;
  3. using Microsoft.AspNetCore.Builder;
  4. using Microsoft.AspNetCore.Hosting;
  5. using Microsoft.Extensions.DependencyInjection;
  6. using Microsoft.Extensions.Hosting;
  7. using Newtonsoft.Json;
  8. using Newtonsoft.Json.Serialization;
  9. using OnceMi.AspNetCore.OSS;
  10. using Serilog;
  11. using Yitter.IdGenerator;
  12. namespace Admin.NET.Web.Core;
  13. public class Startup : AppStartup
  14. {
  15. public void ConfigureServices(IServiceCollection services)
  16. {
  17. // 统一配置项目选项注册
  18. services.AddProjectOptions();
  19. services.AddSqlSugarSetup(App.Configuration);
  20. services.AddJwt<JwtHandler>(enableGlobalAuthorize: true);
  21. services.AddCorsAccessor();
  22. services.AddRemoteRequest();
  23. services.AddTaskScheduler();
  24. services.AddSensitiveDetection();
  25. services.AddControllersWithViews()
  26. .AddMvcFilter<ActionFilter>()
  27. .AddMvcFilter<ResultFilter>()
  28. .AddNewtonsoftJson(options =>
  29. {
  30. options.SerializerSettings.ContractResolver = new CamelCasePropertyNamesContractResolver(); // 响应驼峰命名
  31. options.SerializerSettings.DateFormatString = "yyyy-MM-dd HH:mm:ss"; // 时间格式化
  32. options.SerializerSettings.ReferenceLoopHandling = ReferenceLoopHandling.Ignore; // 忽略循环引用
  33. // options.SerializerSettings.NullValueHandling = NullValueHandling.Ignore; // 忽略空值
  34. })
  35. .AddInjectWithUnifyResult<AdminResultProvider>();
  36. // 注册日志事件订阅者(支持自定义消息队列组件)
  37. services.AddEventBus(builder =>
  38. {
  39. builder.AddSubscriber<LogEventSubscriber>();
  40. });
  41. // 注册OSS对象存储
  42. services.AddOSSService(option =>
  43. {
  44. var ossOptions = App.GetOptions<OSSProviderOptions>();
  45. option.Provider = (OSSProvider)ossOptions.Provider;
  46. option.Endpoint = ossOptions.Endpoint;
  47. option.AccessKey = ossOptions.AccessKey;
  48. option.SecretKey = ossOptions.SecretKey;
  49. option.Region = ossOptions.Region;
  50. option.IsEnableCache = ossOptions.IsEnableCache;
  51. option.IsEnableHttps = ossOptions.IsEnableHttps;
  52. });
  53. // 注册CSRedis缓存
  54. services.AddCSRedisSetup();
  55. // 注册模板引擎
  56. services.AddViewEngine();
  57. // 注册即时通讯
  58. services.AddSignalR();
  59. // 增加Logo输出显示
  60. services.AddLogoDisplay();
  61. }
  62. public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
  63. {
  64. if (env.IsDevelopment())
  65. {
  66. app.UseDeveloperExceptionPage();
  67. }
  68. else
  69. {
  70. app.UseExceptionHandler("/Home/Error");
  71. app.UseHsts();
  72. }
  73. // 添加状态码拦截中间件
  74. app.UseUnifyResultStatusCodes();
  75. app.UseHttpsRedirection();
  76. app.UseStaticFiles();
  77. // Serilog请求日志中间件---必须在 UseStaticFiles 和 UseRouting 之间
  78. app.UseSerilogRequestLogging();
  79. app.UseRouting();
  80. app.UseCorsAccessor();
  81. app.UseAuthentication();
  82. app.UseAuthorization();
  83. app.UseInject();
  84. app.UseEndpoints(endpoints =>
  85. {
  86. // 注册集线器
  87. endpoints.MapHubs();
  88. endpoints.MapControllerRoute(
  89. name: "default",
  90. pattern: "{controller=Home}/{action=Index}/{id?}");
  91. });
  92. // 设置雪花Id算法机器码
  93. YitIdHelper.SetIdGenerator(new IdGeneratorOptions
  94. {
  95. WorkerId = App.GetOptions<SnowIdOptions>().WorkerId
  96. });
  97. }
  98. }