123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105 |
- using Autofac;
- using Autofac.Extensions.DependencyInjection;
- using Microsoft.AspNetCore.Builder;
- using Microsoft.Extensions.Configuration;
- using Microsoft.Extensions.DependencyInjection;
- using Microsoft.Extensions.Hosting;
- using ERP.Framework.Config;
- using ERP.Framework.Constants;
- using ERP.Framework.Extensions;
- using ERP.Framework.Filter;
- using Newtonsoft.Json;
- namespace ERP.Core
- {
- public static class Application
- {
- /// <summary>
- /// 初始化模块项目
- /// </summary>
- /// <param name="builder"></param>
- /// <param name="serviceInfo"></param>
- /// <returns></returns>
- public static WebApplicationBuilder InitApplication(this WebApplicationBuilder builder)
- {
- // 获取配置项
- var redisConfig = builder.Configuration.GetSection(FrameworkConstant.Redis).Get<RedisConfig>();
- //var dbConfig = builder.Configuration.GetSection(FrameworkConstant.DBConfig).Get<DBConfig>();
- // 处理Id生成配置
- builder.Configuration.InitIdGenerater();
- // DI
- builder.Host.UseServiceProviderFactory(new AutofacServiceProviderFactory());
- builder.Host.ConfigureContainer<ContainerBuilder>(builder =>
- {
- builder.InitAutofac();
- });
- // 缓存注册
- builder.Services.AddCache(redisConfig);
- // EntityFrameworkCore
- //builder.Services.AddSqlSugar(dbConfig);
- // 跨域
- builder.Services.AddCorsAccessor();
- // Todo 任务队列
- // Todo 任务调度
- // Todo 添加MiniProfiler 性能分析
- // Swagger
- //builder.Services.AddSwagger();
- // 校验异常拦截
- // 全局异常拦截
- // Json序列化
- builder.Services
- .AddControllers(options =>
- {
- options.Filters.Add(new GlobalActionFilter());
- options.Filters.Add(new GlobalExceptionFilter());
- })
- .AddNewtonsoftJson(options =>
- {
- options.SerializerSettings.DateFormatHandling = DateFormatHandling.IsoDateFormat;
- options.SerializerSettings.DateTimeZoneHandling = DateTimeZoneHandling.Local;
- options.SerializerSettings.DateFormatString = DateTimeConstant.Date_Long;
- options.SerializerSettings.ReferenceLoopHandling = ReferenceLoopHandling.Ignore; //忽略循环引用
- //options.SerializerSettings.ContractResolver = new CamelCasePropertyNamesContractResolver(); //动态对象属性名大写
- //options.SerializerSettings.NullValueHandling = NullValueHandling.Ignore; // 忽略空值
- //options.SerializerSettings.MetadataPropertyHandling = MetadataPropertyHandling.Ignore; // DateTimeOffset 异常
- });
- // 国际化处理
- builder.Services.AddLocalization();
- // Todo 事务总线 Redis消息队列 RabbitMQ 消息队列
- //builder.Services.AddEventBus()
- // Todo AddLog() 日志
- // 可添加 AddOauth2() 第三方授权或SSO
- // AddElasticSerch() 配置ElasticSearc
- // AddRateLimiting 限流
- return builder;
- }
- public static void InitConfigure(this IApplicationBuilder app)
- {
- //app.HandleAuthentication();
- app.UseHttpsRedirection();
- //app.UseI18N();
- //app.UseSwagger();
- //app.UseSwaggerUI();
- app.UseRouting();
- app.UseEndpoints(endpoints =>
- {
- // endpoints.MapMetrics();
- endpoints.MapControllers();
- //endpoints.MapHealthChecks("/health");
- });
- }
- }
- }
|