.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时一些用户自定义服务无法依赖注入。
解决方案
var builder = Host.CreateDefaultBuilder(args);
builder.LoadAssemblies(); //加载全局程序集
/// <summary>
/// 加载程序集
/// </summary>
/// <param name="hostBuilder"></param>
/// <returns></returns>
public static IHostBuilder LoadAssemblies(this IHostBuilder hostBuilder)
{
//显式引用类所在的dll
var T1 = typeof(CSFramework.WebApi.Common.WebApiTools);
var T2 = typeof(CSFrameworkV6.Models.tb_MyUser);
//加载以下程序集(包含所有实体模型、自定义服务的程序集)
GlobalAssemblies.Assemblies = new System.Reflection.Assembly[]
{
System.Reflection.Assembly.Load("CSFramework.WebApi.Common"),
System.Reflection.Assembly.Load("CSFramework.WebApi.Core"),
System.Reflection.Assembly.Load("CSFramework.WebApi.Models"),
System.Reflection.Assembly.Load("CSFramework.WebApi.Interfaces"),
System.Reflection.Assembly.Load("CSFramework.WebApi.Business"),
System.Reflection.Assembly.Load("CSFramework.WebApi"),
System.Reflection.Assembly.Load("CSFrameworkV6.Models"),
};
return hostBuilder;
}
services.AddInjectionServices(); //自动注入服务(注入用户自定义接口)
服务的生命周期
默认的依赖注入容器提供了三种生命周期:
暂时(AddTransient)
每次在向服务容器进行请求时都会创建新的实例,这种生存期适合轻量级、 无状态的服务。
范围内(AddScoped)
为每个客户端请求创建一次实例。
单例(AddSingleton)
第一次请求时(或者在运行Startup.ConfigureServices 并且使用服务注册指定实例时)创建,每个后续请求都使用相同的实例。如果应用需要单例行为,建议允许服务容器管理服务的生存期。 不要再自己实现单例设计模式。

.NET8 WebApi框架接口认证方式:Bearer Token/Jwt Token详解|C/S开发框架
Postman工具设置Bearer Token与调试WebApi接口|C/S开发框架
WebApi框架 | 接口认证方式:Bearer Token|C/S开发框架
.NETCore WebApi框架:从appsettings.json 读取参数配置|C/S开发框架
【原创】WebApi开发框架:Token生成、Token缓存原理、Token验证、令牌机制与原理
.NETCore WebApi后端开发框架+达梦数据库测试报告|C/S开发框架
WebApi NETCore框架 | APIProviderFactory 调用WebApi接口|C/S开发框架
RedGate15|SQLToolbelt Essentials 安装手册|软件下载|C/S开发框架
MySqlConnector.MySqlException:“Loading local data is disabled|C/S开发框架
CSFramework.WebapiV3(.NET7/NetCore) 系统安全及Token令牌介绍|C/S开发框架
The server factory could not be located for the given input: Microsoft.Owin.Host.HttpListener
代码生成器 CodeGenerator V6.1 | 提供两种生成报表策略(表格/标签,TableObject/TextObject)|C/S开发框架
.NETCore WebApi框架request/response模型介绍(req_XXX/res_XXX)|C/S开发框架
XtraReport:annot execute report scripts because the application does not include required libraries
CSFramework.WebApi V3.0 WebApi开发框架(.NETCore+EFCore)
客户管理 (Customer , 基础资料数据维护) | 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内容管理系统 |
在线客服
