最新文章 (全部类别)
运行.NET8 Winform程序弹窗用户账户控制解决方案
使用Xlight FTP文件服务器
CSFramework试用版定制用户软件信息
DevExpress TreeList树形表格组件使用
DevExpress GridView表格不启用/禁用主从表模式(Master/Detail)
CSFrameworkV6开发框架集成GZUpdate.Client自动升级程序
FastReport.NET2023破解版去除水印DEMO VERSION (2023.1.8/2023.2.18版本)
.NET8调用存储过程传入DataTable表类型解决方案
使用DeepSeek生成DevExpress表格、窗体、C#模型代码并集成到开发框架
C# EF+LINQ 查询主表明细表数据查询条件写法
C/S架构软件开发平台 - 旗舰版V6.1 - 序列号管理
塑木地板行业ERP - 用户操作手册 - 物料类别管理
QMS五金制品行业报价系统 - 用户操作手册 - 成本基础资料表
QMS五金制品行业报价系统 - 用户操作手册 - 采购评估
QMS五金制品行业报价系统 - 用户操作手册 - 设备登记
QMS五金制品行业报价系统 - 用户操作手册 - 新品可行性评估
QMS五金制品行业报价系统 - 用户操作手册 - 工艺工序维护
QMS五金制品行业报价系统 - 用户操作手册 - 业务员管理
QMS五金制品行业报价系统 - 用户操作手册 - 客户管理
塑木地板行业ERP - 用户操作手册 - 组织架构(部门)
塑木地板行业ERP - 用户操作手册 - 系统初始化
塑木地板行业ERP - 用户操作手册 - 付款AP
塑木地板行业ERP - 用户操作手册 - 供应商对账单
塑木地板行业ERP - 用户操作手册 - 收款AR
塑木地板行业ERP - 用户操作手册 - 客户对账
塑木地板行业ERP - 用户操作手册 - 销售退货
塑木地板行业ERP - 用户操作手册 - 采购入库
塑木地板行业ERP - 用户操作手册 - 采购退货
塑木地板行业ERP - 用户操作手册 - 采购订单
塑木地板行业ERP - 用户操作手册 - 供应商管理
CSFrameworkV6.0 - 快速开发平台 - 增加字段操作步骤
录入基础资料数据,编码自动生成,自动编号
塑木地板行业ERP - 用户操作手册 - 客户送货单(成品出库)
塑木地板行业ERP - 用户操作手册 - 客户管理
VS2022项目自动刷新依赖项程序集引用(出现黄色叹号)
官方VS2022下载,VS2022密钥
.NETCore WebApi服务器提供本地文件服务(通过URL访问图片)
C/S架构软件开发平台 - 旗舰版V6.0 - 底层框架迭代开发
.NET8 Winform桌面应用程序禁用dpi缩放
GIT改了密码导致无法提交代码解决方案
OneDrive安装闪退解决方案
VS2022正在加载设计器,连接到已命名的管道时发生超时
客户端部署.NET7程序 | 客户端安装环境
DevExpress.XtraTreeList.TreeList 组件自动定位当前记录
塑木地板行业ERP - 用户操作手册 - 软件安装&部署
塑木地板行业ERP - 用户操作手册 - 用户管理
塑木地板行业ERP - 用户操作手册 - 角色管理
塑木地板行业ERP - 用户操作手册 - 成品出入库历史记录
塑木地板行业ERP - 用户操作手册 - 成品出库(客户发货单)
塑木地板行业ERP - 用户操作手册 - 销售订单
.net敏捷开发,创造卓越

.NETCore WebApi运行报错:System.AggregateException:“Some services are not able to be constructed


WebApi运行报错:

.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# 全选
var builder = Host.CreateDefaultBuilder(args);
builder.LoadAssemblies(); //加载全局程序集

 

C# 全选
        /// <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;
        }

 

C# 全选
services.AddInjectionServices(); //自动注入服务(注入用户自定义接口)

 

服务的生命周期

默认的依赖注入容器提供了三种生命周期:

暂时(AddTransient)

每次在向服务容器进行请求时都会创建新的实例,这种生存期适合轻量级、 无状态的服务。

范围内(AddScoped)

为每个客户端请求创建一次实例。

单例(AddSingleton)

第一次请求时(或者在运行Startup.ConfigureServices 并且使用服务注册指定实例时)创建,每个后续请求都使用相同的实例。如果应用需要单例行为,建议允许服务容器管理服务的生存期。 不要再自己实现单例设计模式。

 

版权声明:本文为开发框架文库发布内容,转载请附上原文出处连接
C/S框架网
上一篇:C#.NET各版本常用语法糖大全(C#3~C#10)
下一篇:MySql MySqlBulkLoader批量导入数据(批导BulkInsert)
评论列表

发表评论

评论内容
昵称:
关联文章

.NETCore WebApi运行System.AggregateException:“Some services are not able to be constructed
System.AggregateException:“Some services are not able to be constructed, Unable to resolve service for type 'Microsoft.AspNetCore.Http.IHttpContextAccessor
WebApi日期解析错误:The JSON value could not be converted to System.Nullable`1[System.DateTime].
FastReport:TfrxReportClass cold not be found!
System.NotSupportedException:“The entity or complex type 'CSFramework.EF.tb_MyUser' cannot be constructed in a LINQ to Entities query.”
FastReport.NET System.PlatformNotSupportedException:“Operation is not supported on this platform.”
运行ASP.NET 自承载WebApi服务器:未能加载文件或程序集System.Web.Http或它的某一个依赖项
达梦数据库使用 DmBulkCopy 批导数据:Operation is not valid due to the current state of the object
调用WriteToken方法生成jwttoken:Unable to create KeyedHashAlgorithm for algorithm 'HS256', the key size must be greater than: '256' bits
打开设计窗体:To prevent possible data loss before loading the designer
VS按F5调试WebApi,ID为14740的进程当前未运行
JsonDataConnection.CreateAllTables方法DataSet does not support System.Nullable<>
The server factory could not be located for the given input: Microsoft.Owin.Host.HttpListener
.NETCore使用Encoding.GetEncoding("GBK")解决方案(汉字转拼音)
升级程序DataTable.ReadXml:MySql.Data.Types.MySqlDateTime is not allowed here
HTTP Error 500.31 - Failed to load ASP.NET Core runtime,Microsoft.NetCore.App or Microsoft.AspNetCore.App was not found.
EF.Core:provider: Session Provider, error: 19 - Physical connection is not usable
.NET8/ .NETCore /ASP.NETCore 部署WebApi到IIS服务器需要安装的运行环境
.NETCore使用EF保存图片:OracleExceptiol: ORA-01460:转换请求无法实施或不合理
EF调用OracleSystem.FormatException:“One of the identified items was in an invalid format.”

热门标签
软件著作权登记证书 .NET .NET Reactor .NET5 .NET6 .NET7 .NET8 .NET9 .NETFramework AI编程 APP AspNetCore AuthV3 Auth-软件授权注册系统 Axios B/S B/S开发框架 B/S框架 BSFramework Bug Bug记录 C#加密解密 C#源码 C/S CHATGPT CMS系统 CodeGenerator CSFramework.DB CSFramework.EF CSFramework.License CSFrameworkV1学习版 CSFrameworkV2标准版 CSFrameworkV3高级版 CSFrameworkV4企业版 CSFrameworkV5旗舰版 CSFrameworkV6.0 CSFrameworkV6.1 CSFrameworkV6旗舰版 DAL数据访问层 Database datalock DbFramework DeepSeek Demo教学 Demo实例 Demo下载 DevExpress教程 Docker Desktop DOM ECS服务器 EFCore EF框架 Element-UI EntityFramework ERP ES6 Excel FastReport GIT HR IDatabase IIS JavaScript LINQ MES MiniFramework MIS MySql NavBarControl NETCore Node.JS NPM OMS Oracle资料 ORM PaaS POS Promise API PSD QMS RedGet Redis RSA SAP Schema SEO SEO文章 SQL SQLConnector SQLite SqlServer Swagger TMS系统 Token令牌 VS2022 VSCode VS升级 VUE WCF WebApi WebApi NETCore WebApi框架 WEB开发框架 Windows服务 Winform 开发框架 Winform 开发平台 WinFramework Workflow工作流 Workflow流程引擎 XtraReport 安装环境 版本区别 报表 备份还原 踩坑日记 操作手册 达梦数据库 代码生成器 迭代开发记录 功能介绍 官方软件下载 国际化 基础资料窗体 架构设计 角色权限 开发sce 开发工具 开发技巧 开发教程 开发框架 开发平台 开发指南 客户案例 快速搭站系统 快速开发平台 框架升级 毛衫行业ERP 秘钥 密钥 权限设计 软件报价 软件测试报告 软件加壳 软件简介 软件开发框架 软件开发平台 软件开发文档 软件授权 软件授权注册系统 软件体系架构 软件下载 软件著作权登记证书 软著证书 三层架构 设计模式 生成代码 实用小技巧 视频下载 收钱音箱 数据锁 数据同步 塑木地板行业ERP 微信小程序 未解决问题 文档下载 喜鹊ERP 喜鹊软件 系统对接 详细设计说明书 新功能 信创 行政区域数据库 需求分析 疑难杂症 蝇量级框架 蝇量框架 用户管理 用户开发手册 用户控件 在线支付 纸箱ERP 智能语音收款机 自定义窗体 自定义组件 自动升级程序
联系我们
联系电话:13923396219(微信同号)
电子邮箱:23404761@qq.com
站长微信二维码
微信二维码