.NETCore WebApi运行报错:System.AggregateException:“Some services are not able to be constructed|C/S开发框架
.NETCore WebApi运行报错:System.AggregateException:“Some services are not able to be constructed System.AggregateException:“Some services are not able to be constructed (Error while validating the service descriptor 'ServiceType: CSFramework.WebApi.Business.Demo_CustomerBusiness Lifetime: Transient ImplementationType: CSFramework.WebApi.Business.Demo_CustomerBusiness': Unable to resolve service for type 'CSFramework.WebApi.Core.Interfaces.IUserContext' while attempting to activate 'CSFramework.WebApi.Business.Demo_CustomerBusiness'.) (Error while validating the service descriptor 'ServiceType: CSFramework.WebApi.Business.Demo_Customer_AutoLayoutBusiness Lifetime: Transient ImplementationType: CSFramework.WebApi.Business.Demo_Customer_AutoLayoutBusiness': Unable to resolve service for type 'CSFramework.WebApi.Core.Interfaces.IUserContext' while attempting to activate 'CSFramework.WebApi.Business.Demo_Customer_AutoLayoutBusiness'.) (Error while validating the service descriptor 'ServiceType: CSFramework.WebApi.Business.Demo_INV_AutoLayoutBusiness Lifetime: Transient ImplementationType: CSFramework.WebApi.Business.Demo_INV_AutoLayoutBusiness': Unable to resolve service for type 'CSFramework.WebApi.Core.Interfaces.IUserContext' while attempting to activate 'CSFramework.WebApi.Business.Demo_INV_AutoLayoutBusiness'.) (Error while validating the service descriptor 'ServiceType: CSFramework.WebApi.Business.Demo_INV_BridgesBusiness Lifetime: Transient ImplementationType: CSFramework.WebApi.Business.Demo_INV_BridgesBusiness': Unable to resolve service for type 'CSFramework.WebApi.Core.Interfaces.IUserContext' while attempting to activate 'CSFramework.WebApi.Business.Demo_INV_BridgesBusiness'.) (Error while validating the service descriptor 'ServiceType: CSFramework.WebApi.Business.Product_WebApiOnlyBusiness Lifetime: Transient ImplementationType: CSFramework.WebApi.Business.Product_WebApiOnlyBusiness': Unable to resolve service for type 'CSFramework.WebApi.Core.Interfaces.IUserContext' while attempting to activate 'CSFramework.WebApi.Business.Product_WebApiOnlyBusiness'.) (Error while validating the service descriptor 'ServiceType: CSFramework.WebApi.Business._Demo_CustomerBusiness Lifetime: Transient ImplementationType: CSFramework.WebApi.Business._Demo_CustomerBusiness': Unable to resolve service for type 'CSFramework.WebApi.Core.Interfaces.IUserContext' while attempting to activate 'CSFramework.WebApi.Business._Demo_CustomerBusiness'.) (Error while validating the service descriptor 'ServiceType: CSFramework.WebApi.Business._Demo_INVBusiness Lifetime: Transient ImplementationType: CSFramework.WebApi.Business._Demo_INVBusiness': Unable to resolve service for type 'CSFramework.WebApi.Core.Interfaces.IUserContext' while attempting to activate 'CSFramework.WebApi.Business._Demo_INVBusiness'.) (Error while validating the service descriptor 'ServiceType: CSFramework.WebApi.Business._Demo_ProductBusiness Lifetime: Transient ImplementationType: CSFramework.WebApi.Business._Demo_ProductBusiness': Unable to resolve service for type 'CSFramework.WebApi.Core.Interfaces.IUserContext' while attempting to activate 'CSFramework.WebApi.Business._Demo_ProductBusiness'.) (Error while validating the service descriptor 'ServiceType: CSFramework.WebApi.Business._Demo_SupplierBusiness Lifetime: Transient ImplementationType: CSFramework.WebApi.Business._Demo_SupplierBusiness': Unable to resolve service for type 'CSFramework.WebApi.Core.Interfaces.IUserContext' while attempting to activate 'CSFramework.WebApi.Business._Demo_SupplierBusiness'.) (Error while validating the service descriptor 'ServiceType: CSFramework.WebApi.Business._Demo_YDBusiness Lifetime: Transient ImplementationType: CSFramework.WebApi.Business._Demo_YDBusiness': Unable to resolve service for type 'CSFramework.WebApi.Core.Interfaces.IUserContext' while attempting to activate 'CSFramework.WebApi.Business._Demo_YDBusiness'.)” 原因运行WebApi时一些用户自定义服务无法依赖注入。 解决方案C# 全选
C# 全选
C# 全选
服务的生命周期默认的依赖注入容器提供了三种生命周期: 暂时(AddTransient)每次在向服务容器进行请求时都会创建新的实例,这种生存期适合轻量级、 无状态的服务。 范围内(AddScoped)为每个客户端请求创建一次实例。 单例(AddSingleton)第一次请求时(或者在运行Startup.ConfigureServices 并且使用服务注册指定实例时)创建,每个后续请求都使用相同的实例。如果应用需要单例行为,建议允许服务容器管理服务的生存期。 不要再自己实现单例设计模式。 扫一扫加作者微信
参考文档:
.NETCore WebApi后端开发框架+达梦数据库测试报告|C/S开发框架 .NET8 .NETCore运行环境下载|C/S开发框架 .NET7 .NETCore运行环境下载|C/S开发框架 .NETCore WebApi框架request/response模型介绍(req_XXX/res_XXX)|C/S开发框架 .NETCore WebApi框架:从appsettings.json 读取参数配置|C/S开发框架 .NETCore使用EF保存图片报错:OracleExceptiol: ORA|01460:转换请求无法实施或不合理|C/S开发框架 SystemController – 系统管理控制器 | CSFramework.WebApi后端框架|C/S开发框架 .NET 5.0运行环境安装包下载|C/S开发框架 CSFramework.WebAPI 后端框架系统架构图|C/S开发框架 CSFramework.WebApi后端框架 | 开发者账号(公钥ApiKey与私钥SecretKey)|C/S开发框架 ImageController – 图片资源控制器 | CSFramework.WebApi后端框架|C/S开发框架 系统数据库结构 – CSFramework_WebApi|C/S开发框架 无法升级数据库"CSFramework_WebAPITest",因为它是只读的,它具有只读文件,或是用户无权修改其中的某些文件|C/S开发框架 测试FileController文件控制器 | CSFramework.WebApi后端框架|C/S开发框架 测试ImageController图片文件控制器 | CSFramework.WebApi后端框架|C/S开发框架
其它资料:
什么是C/S结构? | C/S框架核心组成部分 | C/S框架-WebService部署图 | C/S框架-权限管理 | C/S结构系统框架 - 5.1旗舰版介绍 | C/S结构系统框架 - 功能介绍 | C/S结构系统框架 - 产品列表 | C/S结构系统框架 - 应用展示(图) | 三层体系架构详解 | C/S架构轻量级快速开发框架 | C/S框架网客户案例 | WebApi快速开发框架 | C/S框架代码生成器 | 用户授权注册软件系统 | 版本自动升级软件 | 数据库底层应用框架 | CSFramework.CMS内容管理系统 | |