zoukankan      html  css  js  c++  java
  • 在.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。循环依赖问题就这么闪亮登场了。

  • 相关阅读:
    0705. Design HashSet (E)
    VMware简单使用
    Git笔记
    初识MyBatis
    数据库连接池配置 testOnBorrow
    Redis list操作命令
    文件/目录对比:diff命令
    可用于区块链的共识算法
    分布式一致性算法,你确定不了解一下?
    Jmeter文件下载测试
  • 原文地址:https://www.cnblogs.com/dudu/p/6230506.html
Copyright © 2011-2022 走看看