Cannot add reference to project because of a circu

2020-02-09 06:17发布

问题:

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?

回答1:

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.



回答2:

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!



回答3:

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.



回答4:

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.



回答5:

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.



回答6:

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.



回答7:

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.



回答8:

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.