Application.cs 3.8 KB

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