由于循环依赖错误,无法添加对项目的引用
我在应用程序中创建了 2 个虚拟项目,并将它们命名为 BAL
和 DAL
。当我构建它们时,它们构建成功。如果我向 DAL
项目添加对 BAL
的引用,效果会很好。但是,在将 DAL
引用添加到 BAL
项目时,出现以下错误:
无法添加对 DAL 的引用。添加此项目作为引用会导致循环依赖。
谁能帮我解决这个错误?
I created 2 dummy projects in my application and named them BAL
and DAL
. When I build them, they build successfully. If I add a reference to BAL
to the DAL
project, it added nicely. But while adding the DAL
reference to the BAL
project, I get the following error:
A reference to DAL could not be added. Adding this project as a reference would cause a circular dependency.
Can anyone help me to solve this error?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(11)
您需要执行以下操作:
右键单击解决方案资源管理器中的 DAL 项目,然后在上下文菜单中选择项目依赖项。
您现在将看到一个窗口,其中显示 DAL 项目的项目依赖项。确保未选中 BAL。
现在您应该能够添加您的参考...
我希望这会有所帮助,我已尽力使其尽可能简单和直接。
说明:
您的 DAL 应该无法访问 BAL。你的代码引用依赖关系应该是这样的:
MVC 项目 ->巴尔-> DAL
MVC项目应该引用BAL,BAL应该引用DAL。像这样设置你的项目。让它发挥作用,然后你就会更好地理解为什么这个设置更好。
给定:
考虑以下因素:
UI 应该从 BAL 获取其信息,BAL 能够根据 DAL 组合其数据。
Here's what you need to do:
Right click on the DAL Project in the solution explorer and select Project dependencies in the context menu.
You will now see a window that shows the project dependencies of the DAL Project. Make sure that BAL isn't checked.
Now you should be able to add your reference...
I hope this helps I've tried to keep it as simple and straight forward as possible.
Explanation:
Your DAL should not be able to access the BAL. Your code reference dependencies should be like this:
MVC project -> BAL -> DAL
The MVC project should reference the BAL, the BAL should reference the DAL. Set up your project like this. Make it work and then you will better understand why this setup is better.
Given:
Cosider the following:
The UI should get its information from the BAL which could be able to compose it's data based on the DAL.
您只能以一种方式引用,否则您会收到像您所说的错误。只需执行以下操作:删除从 DAL 到 BL 的引用,然后从 BL 到 DAL 创建一个新引用!
You can only reference in one way otherwise you get the error like you said. Just do this: delete the reference from your DAL to your BL and make a new one from your BL to your DAL!
“层”的概念隐含着较高的层依赖于较低的层,而不是相反。如果 2 个“层”相互依赖,则其中一层不高于另一层,它们在任何有意义的意义上都不是层,因此可以被认为处于同一层。同样的基本原则也适用于架构组件或模块,正如 Studio 针对项目依赖项所强制执行的那样。如果您使用这个原则 - 将您的项目视为设计模块,而不是将所有内容都放入一个项目中 - 您将拥有结构良好的代码库,随着规模的增长,它会给您带来更少的问题。
It is implicit in the concept of "layers" that higher layers depend on lower ones, and not the other way round. If 2 "layers" are mutually dependent, then one is not higher than the other, they are not layers in any meaningful sense, and so can be considered to be in the same layer. The same basic principle holds for architectural components or modules, as enforced by Studio for project dependencies. If you use this principle - think of your projects as design modules rather than e.g. just throwing everything into a single project - you will have well-structured codebase which will give you far less problems as it grows in size.
这会导致循环依赖。您可能想要做的是拥有一个主应用程序项目,它引用 BAL,然后 BAL 引用 DAL。
数据访问不需要引用业务逻辑。
That would cause a circular dependency. What you perhaps want to do instead is have a main application project, which references the BAL, and then BAL referenes DAL.
Data access should not need to reference business logic.
这件事刚刚发生在我身上。您有一个循环依赖关系,即两个项目都互相引用。您需要使其中之一独立于另一个。需要一些时间,而且发生得很快。前一秒我还在愉快地编码,下一秒我就出现了 45 个这样的错误。只是花了一些时间,但它也使您的架构/程序结构变得更好,帮助您正确理清依赖关系。
This just happened to me. You have a circular dependency, i.e. two projects both referencing each other. You need to make one of them independent of the other. Takes some time and it happens so quick. One second I was happily coding along, and the next I had 45 errors like this. Just took some time but it makes your architecture/program structure better too, helping you sort out dependencies properly.
有时,您有两个不同的项目,每个项目都需要另一个项目拥有的方法。在这种情况下,您可以创建第三个项目并将共享代码移入其中,或者选择两个项目之一将共享代码放入其中。
Occasionally, you have two different projects, each of which needs methods that the other has. In this case, you can either make a third project and move the shared code into there, or choose one of the two projects to put the shared code in.
当我构建一个具有多个层(如存储库接口层、存储库服务层、sql 服务层、休息服务层和我的主 WPF UI 层)的 WPF 应用程序时,我遇到了这个问题。
不必要地引用其他项目。我删除了这个不必要的
参考。
有我的 WPF UI 项目作为参考(我的 StartUp 项目);这是
是什么创建了循环引用。我删除了这个。
=================================================== ======================
结论:检查每个项目的引用依赖关系,确保没有不必要的引用。确保子层未引用参考中的启动项目。
希望我有帮助。
This problem occurred to me when I was building a WPF application with several layers like repository interface layer, repository service layer, sql service layer, rest service layer and my main WPF UI layer.
unnecessarily referencing other projects. I removed this unnecessary
reference.
had my WPF UI project as reference(My StartUp project); this is
what was creating circular reference. I removed this.
========================================================================
Conclusion: Check each projects reference dependency and make sure there are no unnecessary reference. Make sure sub layers are not referencing the startup project in the reference.
Hope I was helpful.
就我而言,我复制了项目文件,但没有生成新的
ProjectGuid
。由于 Visual Studio 使用ProjectGuid
唯一标识项目,因此它假定该项目正在尝试引用自身。In my case I copied a project file without generating a new
ProjectGuid
. Since Visual Studio uniquely identifies projects using theProjectGuid
, it assumed the project was trying to reference itself.就我而言,该项目已在目标项目的
csproj.cs
中的引用中声明,因此我简单地删除了csproj.cs
中的所有依赖项并尝试从 main 添加再次项目并且成功了in my case the project was somehow already declared in refrences in
csproj.cs
in the targeted project so simply i removed all the dependency incsproj.cs
and tried to add from main project again and it worked我有两个名为
Application
和Persistance
的项目,Application
项目对Persistance
项目的引用遇到了 <代码>循环依赖错误。通过单击
Application
项目并查看其中使用的值,您会注意到Persistance
项目包含在
中> 标记,这就是您收到循环依赖
错误的原因。要修复此错误,只需注释此包含即可。
I had two projects called
Application
andPersistance
that the reference of theApplication
project to thePersistance
project encountered acircular dependency
error.By clicking on the
Application
project and looking at the values used inside it, you will notice that thePersistance
project is include inside the<ItemGroup>
tag, which is why you receive aCircular Dependency
error.To fix this error, just Comment this Include.
要解决此问题,请在构建项目 DLL 后通过浏览来添加引用。不要从“项目”选项卡中选择它。
To get around this, add the reference by browsing to the projects DLL after it has been built. Do not select it from the "Projects" tab.