在.NET Core中遭遇循环依赖问题"A circular dependency was detected"
今天在将一个项目迁移至ASP.NET Core的过程中遭遇一个循环依赖问题,错误信息如下:
A circular dependency was detected for the service of type 'CNBlogs.Application.Interfaces.ITagService'
一开始以为是项目之间的引用关系引起的,在project.json中找来找去,一无所获。
后来从构造函数下手,才发现问题所在。
实现ITagService的类TagService的构造函数是这么定义的:
public class TagService : ITagService
{
private readonly IContentTagsService _contentTagService; public TagService(IContentTagsService contentTagService)
{
_contentTagService = contentTagService;
}
}
这是很标准的通过构造函数依赖注入的定义方式,本身并没有问题。但是我们来看看实现IContentTagsService的类ContentTagsService的构造函数定义:
public class ContentTagsService : IContentTagsService
{
private readonly ITagService _tagService; public ContentTagsService(ITagService tagService)
{
_tagService = tagService;
}
}
TagService实现ITagService,依赖IContentTagsService;ContentTagsService实现IContentTagsService,却又依赖ITagService。循环依赖问题就这么闪亮登场了。
在.NET Core中遭遇循环依赖问题"A circular dependency was detected"的更多相关文章
- 巧用 Lazy 解决.NET Core中的循环依赖关系
原文作者: Thomas Levesque 原文链接:https://thomaslevesque.com/2020/03/18/lazily-resolving-services-to-fix-ci ...
- DAX的圈圈大坑:循环依赖关系错误circular dependency (单表篇)
使用DAX中的某些函数特别类似Calculate这种函数创建计算列时很容易出现一种错误,叫做检测到循环依赖关系,即:A circular dependency was detected.对于刚接触Da ...
- Spring中的循环依赖解决详解
前言 说起Spring中循环依赖的解决办法,相信很多园友们都或多或少的知道一些,但当真的要详细说明的时候,可能又没法一下将它讲清楚.本文就试着尽自己所能,对此做出一个较详细的解读.另,需注意一点,下文 ...
- 面试必杀技,讲一讲Spring中的循环依赖
本系列文章: 听说你还没学Spring就被源码编译劝退了?30+张图带你玩转Spring编译 读源码,我们可以从第一行读起 你知道Spring是怎么解析配置类的吗? 配置类为什么要添加@Configu ...
- 面试阿里,腾讯,字节跳动90%都会被问到的Spring中的循环依赖
前言 Spring中的循环依赖一直是Spring中一个很重要的话题,一方面是因为源码中为了解决循环依赖做了很多处理,另外一方面是因为面试的时候,如果问到Spring中比较高阶的问题,那么循环依赖必定逃 ...
- 从一部电影史上的趣事了解 Spring 中的循环依赖问题
title: 从一部电影史上的趣事了解 Spring 中的循环依赖问题 date: 2021-03-10 updated: 2021-03-10 categories: Spring tags: Sp ...
- 【Spring】Spring中的循环依赖及解决
什么是循环依赖? 就是A对象依赖了B对象,B对象依赖了A对象. 比如: // A依赖了B class A{ public B b; } // B依赖了A class B{ public A a; } ...
- 一起来踩踩 Spring 中这个循环依赖的坑
1. 前言 2. 典型场景 3. 什么是依赖 4. 什么是依赖调解 5. 为什么要依赖注入 6. Spring的依赖注入模型 7. 非典型问题 参考资料 1. 前言 这两天工作遇到了一个挺有意思的Sp ...
- Spring中的循环依赖
循环依赖 在使用Spring时,如果主要采用基于构造器的依赖注入方式,则可能会遇到循环依赖的情况,简而言之就是Bean A的构造器依赖于Bean B,Bean B的构造器又依赖于Bean A.在这种情 ...
随机推荐
- JS模块化
一.原始写法 /* 模块就是实现特定功能的一组方法. 只要把不同的函数(以及记录状态的变量)简单地放在一起,就算是一个模块. 上面的函数m1()和m2(),组成一个模块.使用的时候,直接调用就行了. ...
- Codeigniter的Redis使用
1. ./config/redis.php: <?php $config['redis_host'] = '127.0.0.1'; $config['redis_port'] = '6379'; ...
- PyAutoGUI 简介
转载来自: https://muxuezi.github.io/posts/doc-pyautogui.html http://blog.topspeedsnail.com/archives/5373 ...
- eclipse按照svn插件
在线安装: (1).点击 Help --> Install New Software... (2).在弹出的窗口中点击add按钮,输入Name(任意)和Location(插件的URL),点击OK ...
- Java 之 常用类(二)
1.StringBuffer a.StringBuffer 与 String:①StringBuffer是一个全新的类型,与String没有继承关系 ②StringBuffer的出现是为了解决Stri ...
- [算法总结]three-way partition
procedure three-way-partition(A : array of values, mid : value): i ← 0 j ← 0 n ← size of A - 1 while ...
- js数组操作大全
原文(http://www.cnblogs.com/webhotel/archive/2010/12/21/1912732.html) 用 js有很久了,但都没有深究过js的数组形式.偶尔用用也就是简 ...
- C#开发中常用方法3------Cookie的存取
---------------------------------------------------------------------------------------------------- ...
- 《程序员的自我修养》读书笔记 - dllimport
Math.c { __declspec (dllexport) double Add (xx, xx) {...}} MathApp.c { __declspec(dllimport) doub ...
- linux菜鸟日记(4)
使用一个简单的for循环和if判断语句实现某个网段内所有ping所有客户机的shell程序: ..} do >&; then echo " ${i}通" else e ...