Application.cs 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  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.Core.Context;
  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. // 缓存注册
  37. builder.Services.AddCache(redisConfig);
  38. // EntityFrameworkCore
  39. builder.Services.AddEntityFrameworkMySql();
  40. // 跨域
  41. builder.Services.AddCorsAccessor();
  42. // Todo 任务队列
  43. // Todo 任务调度
  44. // Todo 添加MiniProfiler 性能分析
  45. // Swagger
  46. builder.Services.AddSwaggerGen();
  47. //builder.Services.AddSwagger();
  48. // 校验异常拦截
  49. // 全局异常拦截
  50. // Json序列化
  51. builder.Services
  52. .AddControllers(options =>
  53. {
  54. options.Filters.Add(new GlobalActionFilter());
  55. options.Filters.Add(new GlobalExceptionFilter());
  56. })
  57. .AddNewtonsoftJson(options =>
  58. {
  59. options.SerializerSettings.DateFormatHandling = DateFormatHandling.IsoDateFormat;
  60. options.SerializerSettings.DateTimeZoneHandling = DateTimeZoneHandling.Local;
  61. options.SerializerSettings.DateFormatString = DateTimeConstant.Date_Long;
  62. options.SerializerSettings.ReferenceLoopHandling = ReferenceLoopHandling.Ignore; //忽略循环引用
  63. //options.SerializerSettings.ContractResolver = new CamelCasePropertyNamesContractResolver(); //动态对象属性名大写
  64. //options.SerializerSettings.NullValueHandling = NullValueHandling.Ignore; // 忽略空值
  65. //options.SerializerSettings.MetadataPropertyHandling = MetadataPropertyHandling.Ignore; // DateTimeOffset 异常
  66. });
  67. // 国际化处理
  68. builder.Services.AddLocalization();
  69. // Todo 事务总线 Redis消息队列 RabbitMQ 消息队列
  70. //builder.Services.AddEventBus()
  71. // Todo AddLog() 日志
  72. // 可添加 AddOauth2() 第三方授权或SSO
  73. // AddElasticSerch() 配置ElasticSearc
  74. // AddRateLimiting 限流
  75. builder.Services.AddEndpointsApiExplorer();
  76. builder.Services.AddDbContext<CoreDbContext>();
  77. return builder;
  78. }
  79. public static void InitConfigure(this IApplicationBuilder app)
  80. {
  81. //app.HandleAuthentication();
  82. app.UseHttpsRedirection();
  83. //app.UseI18N();
  84. app.UseSwagger();
  85. app.UseSwaggerUI();
  86. app.UseRouting();
  87. app.UseEndpoints(endpoints =>
  88. {
  89. // endpoints.MapMetrics();
  90. endpoints.MapControllers();
  91. });
  92. }
  93. }
  94. }