Application.cs 4.1 KB

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