惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

酷 壳 – CoolShell
酷 壳 – CoolShell
H
Hacker News: Front Page
P
Palo Alto Networks Blog
T
ThreatConnect
Apple Machine Learning Research
Apple Machine Learning Research
博客园_首页
T
True Tiger Recordings
P
Privacy & Cybersecurity Law Blog
B
Blog
IT之家
IT之家
Last Week in AI
Last Week in AI
F
Full Disclosure
Hacker News: Ask HN
Hacker News: Ask HN
C
Comments on: Blog
Microsoft Azure Blog
Microsoft Azure Blog
C
Cybersecurity and Infrastructure Security Agency CISA
Microsoft Security Blog
Microsoft Security Blog
博客园 - 【当耐特】
N
News and Events Feed by Topic
NISL@THU
NISL@THU
腾讯CDC
雷峰网
雷峰网
Security Latest
Security Latest
李成银的技术随笔
M
Microsoft Research Blog - Microsoft Research
L
LangChain Blog
L
Lohrmann on Cybersecurity
cs.CL updates on arXiv.org
cs.CL updates on arXiv.org
C
Check Point Blog
Y
Y Combinator Blog
Recent Announcements
Recent Announcements
博客园 - Franky
N
News | PayPal Newsroom
V
V2EX
A
About on SuperTechFans
The Register - Security
The Register - Security
月光博客
月光博客
奇客Solidot–传递最新科技情报
奇客Solidot–传递最新科技情报
Google Online Security Blog
Google Online Security Blog
MyScale Blog
MyScale Blog
Cisco Talos Blog
Cisco Talos Blog
Vercel News
Vercel News
WordPress大学
WordPress大学
C
Cyber Attacks, Cyber Crime and Cyber Security
The Hacker News
The Hacker News
IntelliJ IDEA : IntelliJ IDEA – the Leading IDE for Professional Development in Java and Kotlin | The JetBrains Blog
IntelliJ IDEA : IntelliJ IDEA – the Leading IDE for Professional Development in Java and Kotlin | The JetBrains Blog
爱范儿
爱范儿
A
Arctic Wolf
L
LINUX DO - 最新话题
freeCodeCamp Programming Tutorials: Python, JavaScript, Git & More

博客园 - LoveCoder

一个HTML小工具,可以显示你在网页上触摸点击的轨迹、位置、按压时长以及你触发的所有JS事件 SQL Server查询一个很大字段varchar字段的字符串被截断问题 sql server 每个表占用大小查询【转】 电商ERP系统源码出售 kubernetes镜像拉取失败解决方法 ErrImagePull 淘宝虚拟商品自动发货接口 如何排查线上w3wp.exe CPU高的问题,使用到了WinDbg、Visual studio来分析IIS进程池的.dmp文件 sql server 将数据库表里面的数据,转为insert语句,方便小批量转移数据 淘宝库存更新修改接口 淘宝开放平台上货接口,淘宝商品发布接口 C# 模拟http请求出现 由于系统缓冲区空间不足或队列已满,不能执行套接字上的操作[windows服务器] 淘宝订单信息获取接口API,淘宝打单发货接口 使用win-acme在windows+iis服务器下配置自动续期SSL证书【转】 开发Android应用程序,在Android10的系统上提示网络出错? android 反编译APK取源代码。 在.net core使用Serilog,只要简单的三步 淘宝订单信息获取接口API,淘宝打单发货接口 淘宝订单信息获取接口,淘宝订单信息获取API win10远程登录的账号密码
.net7(.net core) 依赖注入:从 AddSingleton 注册的类里面访问 AddScoped 的问题
LoveCoder · 2023-12-01 · via 博客园 - LoveCoder

记录一下以免忘记。

今天从NopCommerce开源项目里面把它的任务调度类拆出来到我的项目用的时候,发现报错,报错信息如下

Some services are not able to be constructed (Error while validating the service descriptor 'ServiceType: OUC.Services.ScheduleTasks.ITaskScheduler Lifetime: Singleton ImplementationType: OUC.Services.ScheduleTasks.TaskScheduler': Cannot consume scoped service 'OUC.Services.ScheduleTasks.IScheduleTaskService' from singleton 'OUC.Services.ScheduleTasks.ITaskScheduler'.)
---> System.InvalidOperationException: Error while validating the service descriptor 'ServiceType: OUC.Services.ScheduleTasks.ITaskScheduler Lifetime: Singleton ImplementationType: OUC.Services.ScheduleTasks.TaskScheduler': Cannot consume scoped service 'OUC.Services.ScheduleTasks.IScheduleTaskService' from singleton 'OUC.Services.ScheduleTasks.ITaskScheduler'.
---> System.InvalidOperationException: Cannot consume scoped service 'OUC.Services.ScheduleTasks.IScheduleTaskService' from singleton 'OUC.Services.ScheduleTasks.ITaskScheduler'.

依赖注册的信息如下:

它在 TaskScheduler(单例注册)的构造函数中使用了 IScheduleTaskService ,而 IScheduleTaskService是用scoped注册的,我把这些代码拆到我的项目后,编译后运行竟然直接报错了,而它的却没有。

后来发现它默认用的是autofac,而我用的是.net core的依赖注入,然后github上有人对autofac这个bug进行了报告 https://github.com/autofac/Autofac.Extensions.DependencyInjection/issues/106

简单来说就是:autofac认为从单例的类访问scoped的类没问题,而.net core自带的依赖注入却不行

回到NopCommerce,它默认用的是autofac,而我项目没用,用的是.net core默认的。所以就报错了