在.NET Core中遭遇循环依赖问题"A circular dependency was detected"
2016-12-28 18:18
495 查看
今天在将一个项目迁移至ASP.NET Core的过程中遭遇一个循环依赖问题,错误信息如下:
一开始以为是项目之间的引用关系引起的,在project.json中找来找去,一无所获。
后来从构造函数下手,才发现问题所在。
实现ITagService的类TagService的构造函数是这么定义的:
这是很标准的通过构造函数依赖注入的定义方式,本身并没有问题。但是我们来看看实现IContentTagsService的类ContentTagsService的构造函数定义:
TagService实现ITagService,依赖IContentTagsService;ContentTagsService实现IContentTagsService,却又依赖ITagService。循环依赖问题就这么闪亮登场了。
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。循环依赖问题就这么闪亮登场了。
相关文章推荐
- because it is included into a circular dependency循环依赖的解决办法
- 安装install DBD::ODBC时出现循环依赖错误Recursive dependency detected:
- [Java]because it is included into a circular dependency(循环导入依赖)
- SPRING循环依赖(circular reference)的解决方法 [转贴]
- 问题解决:javax.servlet.jsp.el.ELException: The "." operator was supplied with an index value of type "java.lang.String" to be applie
- 解决VS2012 Express的"There was a problem sending the command to the program"问题
- 关于String字符串的""值问题以及for增强循环
- 解决 apache error 指定网络名不再可用 "an unknown filter was not added: DEFLATE"的问题
- 安装VS2003时,"Setup Has Detected That Another Program Requires the Computer to Reboot"问题的解决方案
- chrome浏览器中解决embed标签 loop="true" 背景音乐无法循环的问题。
- ipv6设置 -----"eth0: duplicate address detected!" 检测到重复地址问题
- ipv6设置 -----"eth0: duplicate address detected!" 检测到重复地址问题
- sql之解决数据库表的循环依赖问题
- 解决在arm2440休眠唤醒后出现大量类似"**>> yaffs chunk 123 was not erased"的问题
- MVC 请求参数中带有HTML会引发Validation异常 ("A potentially dangerous Request.Form value was detected from the client")
- loaded the "controller" nib but the view outlet was not set.问题解决
- [经验总结]解决Ubuntu系统中deb包循环依赖的问题
- 解决RPM包循环依赖的问题
- 解决ASP.NET 4.0 "A potentially dangerous Request.Form value was detected from the client". 错误
- 彻底解决"The last packet successfully received from the server was * milliseconds ago"问题