由于循环依赖错误,无法添加对项目的引用

发布于 2024-11-30 04:51:31 字数 286 浏览 9 评论 0原文

我在应用程序中创建了 2 个虚拟项目,并将它们命名为 BALDAL。当我构建它们时,它们构建成功。如果我向 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(11

等风来 2024-12-07 04:51:31

您需要执行以下操作:

  1. 右键单击解决方案资源管理器中的 DAL 项目,然后在上下文菜单中选择项目依赖项

  2. 您现在将看到一个窗口,其中显示 DAL 项目的项目依赖项。确保未选中 BAL。

现在您应该能够添加您的参考...

我希望这会有所帮助,我已尽力使其尽可能简单和直接。

说明:

您的 DAL 应该无法访问 BAL。你的代码引用依赖关系应该是这样的:

MVC 项目 ->巴尔-> DAL

MVC项目应该引用BAL,BAL应该引用DAL。像这样设置你的项目。让它发挥作用,然后你就会更好地理解为什么这个设置更好。

给定:

  1. 数据 = 原始数字和字符串
  2. 信息 = 将数据处理成有意义的内容

考虑以下因素:
UI 应该从 BAL 获取其信息,BAL 能够根据 DAL 组合其数据

Here's what you need to do:

  1. Right click on the DAL Project in the solution explorer and select Project dependencies in the context menu.

  2. 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:

  1. Data = raw numbers and strings
  2. Information = processed data into something meaningful

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.

残月升风 2024-12-07 04:51:31

您只能以一种方式引用,否则您会收到像您所说的错误。只需执行以下操作:删除从 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!

匿名的好友 2024-12-07 04:51:31

“层”的概念隐含着较高的层依赖于较低的层,而不是相反。如果 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.

彩扇题诗 2024-12-07 04:51:31

这会导致循环依赖。您可能想要做的是拥有一个主应用程序项目,它引用 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.

悲凉≈ 2024-12-07 04:51:31

这件事刚刚发生在我身上。您有一个循环依赖关系,即两个项目都互相引用。您需要使其中之一独立于另一个。需要一些时间,而且发生得很快。前一秒我还在愉快地编码,下一秒我就出现了 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.

甜柠檬 2024-12-07 04:51:31

有时,您有两个不同的项目,每个项目都需要另一个项目拥有的方法。在这种情况下,您可以创建第三个项目并将共享代码移入其中,或者选择两个项目之一将共享代码放入其中。

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.

醉梦枕江山 2024-12-07 04:51:31

当我构建一个具有多个层(如存储库接口层、存储库服务层、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.

  • I resolved this error. I noticed that some of the layers were
    unnecessarily referencing other projects. I removed this unnecessary
    reference.
  • Then I noticed that some of my service layer and repository layer
    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.

青柠芒果 2024-12-07 04:51:31

就我而言,我复制了项目文件,但没有生成新的 ProjectGuid。由于 Visual Studio 使用 ProjectGuid 唯一标识项目,因此它假定该项目正在尝试引用自身。

In my case I copied a project file without generating a new ProjectGuid. Since Visual Studio uniquely identifies projects using the ProjectGuid, it assumed the project was trying to reference itself.

这个俗人 2024-12-07 04:51:31

就我而言,该项目已在目标项目的 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 in csproj.cs and tried to add from main project again and it worked

烧了回忆取暖 2024-12-07 04:51:31

我有两个名为 ApplicationPersistance 的项目,Application 项目对 Persistance 项目的引用遇到了 <代码>循环依赖错误。
通过单击 Application 项目并查看其中使用的值,您会注意到 Persistance 项目包含在 中> 标记,这就是您收到循环依赖错误的原因。
要修复此错误,只需注释此包含即可。

I had two projects called Application and Persistance that the reference of the Application project to the Persistance project encountered a circular dependency error.
By clicking on the Application project and looking at the values used inside it, you will notice that the Persistance project is include inside the <ItemGroup> tag, which is why you receive a Circular Dependency error.
To fix this error, just Comment this Include.

|煩躁 2024-12-07 04:51:31

要解决此问题,请在构建项目 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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文