//运行时调用此方法。使用此方法向容器添加服务。 // This method gets called by the runtime. Use this method to add services to the container. public void ConfigureServices(IServiceCollection services) { //注册appSettings读取类 // services.AddSingleton(new AppSettings(Configuration)); //注册服务类 services.AddSingleton <IUserService, UserService>(); services.AddSingleton <IBaseRepository <Kernel_User>, BaseRepository <Kernel_User> >(); services.AddSingleton <IMemberService, MemberService>(); services.AddSingleton <IBaseRepository <Kernel_Member>, BaseRepository <Kernel_Member> >(); // services.AddSingleton<IUserRepository, UserRepository>(); //注册Session services.AddSingleton <SessionManager>(); //注入缓存 services.AddMemoryCache(); //注入 HTTPCONTEXT services.AddHttpContextAccessor(); //注册Swagger services.AddSwaggerSetup(); services.AddControllers(); //注册REDIS 服务 RedisServer.Initalize(); }
// This method gets called by the runtime. Use this method to add services to the container. public void ConfigureServices(IServiceCollection services) { #region 跨域设置 services.AddCors(c => { c.AddPolicy("LimitRequests", policy => { policy .WithOrigins(AppSettings.Configuration["Startup:AllowOrigins"].Split('|')) .AllowAnyHeader()//Ensures that the policy allows any header. .AllowAnyMethod(); }); }); #endregion #region 自动映射 services.AddScoped <IMapper, ServiceMapper>(); #endregion #region 说明文档 services.AddSwaggerGen(c => { c.SwaggerDoc("v1", new OpenApiInfo { Version = "v1", Title = $"{AppSettings.Configuration["Startup:ApiName"]} 接口文档", Description = $"{AppSettings.Configuration["Startup:ApiName"]} HTTP API " }); try { //就是这里 var xmlPath = Path.Combine(AppContext.BaseDirectory, "Meiam.System.Hostd.xml"); //这个就是刚刚配置的xml文件名 c.IncludeXmlComments(xmlPath, true); //默认的第二个参数是false,这个是controller的注释,记得修改 } catch (Exception ex) { Console.WriteLine($"Xml 文件丢失,请检查并拷贝。\n{ ex.Message}"); } // 开启加权小锁 c.OperationFilter <AppendAuthorizeFilter>(); }); #endregion #region 配置Json格式 services.AddMvc().AddNewtonsoftJson(options => { // 忽略循环引用 options.SerializerSettings.ReferenceLoopHandling = ReferenceLoopHandling.Ignore; // 不使用驼峰 //options.SerializerSettings.ContractResolver = new DefaultContractResolver(); // 设置时间格式 options.SerializerSettings.DateFormatString = "yyyy-MM-dd HH:mm:ss"; // 如字段为null值,该字段不会返回到前端 //options.SerializerSettings.NullValueHandling = NullValueHandling.Ignore; }); #endregion #region 获取客户端 IP services.Configure <ForwardedHeadersOptions>(options => { options.ForwardedHeaders = ForwardedHeaders.XForwardedFor | ForwardedHeaders.XForwardedProto; options.KnownNetworks.Clear(); options.KnownProxies.Clear(); }); #endregion #region 加载项 //注入缓存 services.AddMemoryCache(); //注入 HTTPCONTEXT services.AddHttpContextAccessor(); //注入 TokenManager services.AddScoped <TokenManager>(); //注入实体映射服务 services.AddScoped <IMapper, ServiceMapper>(); //注入短信服务 services.AddSingleton <IAliyunSmsServices, AliyunSmsServices>(); //注入全局异常过滤 services.AddControllers(options => { //全局异常过滤 options.Filters.Add <GlobalExceptions>(); //全局日志 options.Filters.Add <GlobalActionMonitor>(); }) .ConfigureApiBehaviorOptions(options => { //抑制系统自带模型验证 options.SuppressModelStateInvalidFilter = true; }); //开启计划任务 services.AddTaskSchedulers(); //注册REDIS 服务 RedisServer.Initalize(); #endregion }
// This method gets called by the runtime. Use this method to add services to the container. public void ConfigureServices(IServiceCollection services) { #region 务注入 //启用数据库链接 services.AddSqlsugarSetup(); //跨域设置 services.AddCorsSetup(); //说明文档 services.AddSwaggerSetup(); //注入Cap (EventBus) 按需引入 //services.AddCapSetup(); //注入缓存 services.AddMemoryCache(); //注入 HTTPCONTEXT services.AddHttpContextAccessor(); //注入 TokenManager services.AddScoped <TokenManager>(); //注入实体映射服务 services.AddScoped <IMapper, ServiceMapper>(); //注入短信服务 services.AddSingleton <IAliyunSmsServices, AliyunSmsServices>(); //注册REDIS 服务 RedisServer.Initalize(); //开启计划任务 services.AddTaskSchedulers(); #endregion #region 全局设置 //配置Json格式 services.AddMvc().AddNewtonsoftJson(options => { // 忽略循环引用 options.SerializerSettings.ReferenceLoopHandling = ReferenceLoopHandling.Ignore; // 不使用驼峰 //options.SerializerSettings.ContractResolver = new DefaultContractResolver(); // 设置时间格式 options.SerializerSettings.DateFormatString = "yyyy-MM-dd HH:mm:ss"; // 如字段为null值,该字段不会返回到前端 //options.SerializerSettings.NullValueHandling = NullValueHandling.Ignore; }); //获取客户端 IP services.Configure <ForwardedHeadersOptions>(options => { options.ForwardedHeaders = ForwardedHeaders.XForwardedFor | ForwardedHeaders.XForwardedProto; options.KnownNetworks.Clear(); options.KnownProxies.Clear(); }); //注入全局异常过滤 services.AddControllers(options => { //全局异常过滤 options.Filters.Add <GlobalExceptions>(); //全局日志 options.Filters.Add <GlobalActionMonitor>(); }) .ConfigureApiBehaviorOptions(options => { //抑制系统自带模型验证 options.SuppressModelStateInvalidFilter = true; }); #endregion }