Startup.cs 4.5 KB

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