Application.cs 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  1. using Autofac;
  2. using Autofac.Extensions.DependencyInjection;
  3. using Microsoft.AspNetCore.Builder;
  4. using Microsoft.Extensions.Configuration;
  5. using Microsoft.Extensions.DependencyInjection;
  6. using Microsoft.Extensions.Hosting;
  7. using ERP.Framework.Config;
  8. using ERP.Framework.Constants;
  9. using ERP.Framework.Extensions;
  10. using ERP.Framework.Filter;
  11. using Newtonsoft.Json;
  12. using ERP.Framework.Middleware;
  13. namespace ERP.Core
  14. {
  15. public static class Application
  16. {
  17. /// <summary>
  18. /// 初始化模块项目
  19. /// </summary>
  20. /// <param name="builder"></param>
  21. /// <param name="serviceInfo"></param>
  22. /// <returns></returns>
  23. public static WebApplicationBuilder InitApplication(this WebApplicationBuilder builder)
  24. {
  25. // 获取配置项
  26. var redisConfig = builder.Configuration.GetSection(FrameworkConstant.REDIS).Get<RedisConfig>();
  27. //var dbConfig = builder.Configuration.GetSection(FrameworkConstant.DBConfig).Get<DBConfig>();
  28. // 处理Id生成配置
  29. builder.Configuration.InitIdGenerater();
  30. // DI
  31. builder.Host.UseServiceProviderFactory(new AutofacServiceProviderFactory());
  32. builder.Host.ConfigureContainer<ContainerBuilder>(builder =>
  33. {
  34. builder.InitAutofac();
  35. });
  36. // 注入HttpContext
  37. builder.Services.AddHttpContextAccessor();
  38. // 缓存注册
  39. builder.Services.AddCache(redisConfig);
  40. // EntityFrameworkCore
  41. builder.Services.AddEntityFrameworkMySql();
  42. // 跨域
  43. builder.Services.AddCorsAccessor();
  44. // Todo 任务队列
  45. // Todo 任务调度
  46. // Todo 添加MiniProfiler 性能分析
  47. // Swagger
  48. builder.Services.AddSwagger();
  49. // 校验异常拦截
  50. // 全局异常拦截
  51. // Json序列化
  52. builder.Services
  53. .AddControllers(options =>
  54. {
  55. options.Filters.Add(new GlobalActionFilter());
  56. options.Filters.Add(new GlobalExceptionFilter());
  57. })
  58. .AddNewtonsoftJson(options =>
  59. {
  60. options.SerializerSettings.DateFormatHandling = DateFormatHandling.IsoDateFormat;
  61. options.SerializerSettings.DateTimeZoneHandling = DateTimeZoneHandling.Local;
  62. options.SerializerSettings.DateFormatString = DateTimeConstant.DATE_LONG;
  63. options.SerializerSettings.ReferenceLoopHandling = ReferenceLoopHandling.Ignore; //忽略循环引用
  64. //options.SerializerSettings.ContractResolver = new CamelCasePropertyNamesContractResolver(); //动态对象属性名大写
  65. //options.SerializerSettings.NullValueHandling = NullValueHandling.Ignore; // 忽略空值
  66. //options.SerializerSettings.MetadataPropertyHandling = MetadataPropertyHandling.Ignore; // DateTimeOffset 异常
  67. });
  68. // 国际化处理
  69. builder.Services.AddLocalization();
  70. // Todo 事务总线 Redis消息队列 RabbitMQ 消息队列
  71. //builder.Services.AddEventBus()
  72. // Todo AddLog() 日志
  73. // 可添加 AddOauth2() 第三方授权或SSO
  74. // AddElasticSerch() 配置ElasticSearc
  75. // AddRateLimiting 限流
  76. builder.Services.AddEndpointsApiExplorer();
  77. // 权鉴中间件
  78. builder.Services.AddScoped<AuthenticationMiddleware>();
  79. builder.Services.AddDbContext<CoreDbContext>();
  80. return builder;
  81. }
  82. public static void InitConfigure(this IApplicationBuilder app)
  83. {
  84. app.UseAuthenticationMiddleware();
  85. app.UseHttpsRedirection();
  86. //app.UseI18N();
  87. app.UseSwagger();
  88. app.UseSwaggerUI();
  89. app.UseRouting();
  90. app.UseEndpoints(endpoints =>
  91. {
  92. // endpoints.MapMetrics();
  93. endpoints.MapControllers();
  94. });
  95. }
  96. }
  97. }