Startup.cs 3.9 KB

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