Startup.cs 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  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.AddControllersWithViews()
  25. .AddMvcFilter<RequestActionFilter>()
  26. .AddNewtonsoftJson(options =>
  27. {
  28. options.SerializerSettings.ContractResolver = new CamelCasePropertyNamesContractResolver(); // 响应驼峰命名
  29. options.SerializerSettings.DateFormatString = "yyyy-MM-dd HH:mm:ss"; // 时间格式化
  30. options.SerializerSettings.ReferenceLoopHandling = ReferenceLoopHandling.Ignore; // 忽略循环引用
  31. // options.SerializerSettings.NullValueHandling = NullValueHandling.Ignore; // 忽略空值
  32. })
  33. .AddInjectWithUnifyResult<AdminResultProvider>();
  34. // 注册日志事件订阅者(支持自定义消息队列组件)
  35. services.AddEventBus(builder =>
  36. {
  37. builder.AddSubscriber<LogEventSubscriber>();
  38. });
  39. // 注册OSS对象存储
  40. services.AddOSSService(option =>
  41. {
  42. var ossOptions = App.GetOptions<OSSProviderOptions>();
  43. option.Provider = (OSSProvider)ossOptions.Provider;
  44. option.Endpoint = ossOptions.Endpoint;
  45. option.AccessKey = ossOptions.AccessKey;
  46. option.SecretKey = ossOptions.SecretKey;
  47. option.Region = ossOptions.Region;
  48. option.IsEnableCache = ossOptions.IsEnableCache;
  49. option.IsEnableHttps = ossOptions.IsEnableHttps;
  50. });
  51. // 注册CSRedis缓存
  52. services.AddCSRedisSetup();
  53. // 注册模板引擎
  54. services.AddViewEngine();
  55. // 注册即时通讯
  56. services.AddSignalR();
  57. // 增加Logo输出显示
  58. services.AddLogoDisplay();
  59. }
  60. public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
  61. {
  62. if (env.IsDevelopment())
  63. {
  64. app.UseDeveloperExceptionPage();
  65. }
  66. else
  67. {
  68. app.UseExceptionHandler("/Home/Error");
  69. app.UseHsts();
  70. }
  71. // 添加状态码拦截中间件
  72. app.UseUnifyResultStatusCodes();
  73. app.UseHttpsRedirection();
  74. app.UseStaticFiles();
  75. // Serilog请求日志中间件---必须在 UseStaticFiles 和 UseRouting 之间
  76. app.UseSerilogRequestLogging();
  77. app.UseRouting();
  78. app.UseCorsAccessor();
  79. app.UseAuthentication();
  80. app.UseAuthorization();
  81. app.UseInject();
  82. app.UseEndpoints(endpoints =>
  83. {
  84. // 注册集线器
  85. endpoints.MapHubs();
  86. endpoints.MapControllerRoute(
  87. name: "default",
  88. pattern: "{controller=Home}/{action=Index}/{id?}");
  89. });
  90. // 设置雪花Id算法机器码
  91. YitIdHelper.SetIdGenerator(new IdGeneratorOptions
  92. {
  93. WorkerId = App.GetOptions<SnowIdOptions>().WorkerId
  94. });
  95. }
  96. }