I ran into a problem today, with VS.NET and references between projects. In the development project that I am working on at the moment we have two main C# classes that are called by various classic ASP pages and client side code.

Because both of the classes have attendant helper classes and they are logically separate (and also being coded by two different people) we have them in two separate VS.NET projects. Until today it all worked fine.

Today however we broke things. Class A was already calling Class B, but in a change today we decided that Class B then had to call back into Class A.

Until today the project for Class A had a reference to Class B, not the other way around. When we tried to add a reference to Class B’s project back to Class A’s we got an error complaining that it could not be done because it would create a circular reference.

I guess the problem is that the two projects get compiled to two separate assemblies and that one can’t be built before the other because they both embed meta data from the other.

We scratched our heads for a while, it didn’t help that it was at the end of the day, and came up with the following options:

  1. move the two classes into the same project (this would be less than ideal two people need to work on the code at the same time and it would also stop us from shipping one of the components without the other)
  2. abandon the VS.NET build process for these two components and build them together into the same assembly using the commandline tools (still has the problem of not being able to ship them separately and complicates the build/debug process)
  3. create another class that sits above A and B that makes calls into A and B (A still then needs to call B, but at least B doesn’t then have to call A)

At the moment it looks like we are going to go with option 3, which means some more work for me tomorrow. I hope I can do it cleanly without duplicating code from A and B into the third class.

So, something to look out for in my next .NET project (in our defence this project wasn’t really designed with .NET in mind).

Edit:

I have found another and I believe, better solution. I have created a interface in a separate project (that matches Class A’s current public interface). Class A can then implement the interface and pass a reference to itself cast as the interface when it calls Class B. Class B can then call back to into Class A via the interface.

Class A and B then need reference to the project with the interface, but crucially Class B doesn’t need a reference to Class A.