码迷,mamicode.com
首页 > Web开发 > 详细

在.NET Core中遭遇循环依赖问题"A circular dependency was detected"

时间:2016-12-28 20:16:53      阅读:755      评论:0      收藏:0      [点我收藏+]

标签:今天   .net   项目迁移   log   实现   depend   int   content   public   

今天在将一个项目迁移至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"

标签:今天   .net   项目迁移   log   实现   depend   int   content   public   

原文地址:http://www.cnblogs.com/dudu/p/6230506.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!