I have two projects in a solution named ProjectA (ConsoleApplication) and ProjectB (ClassLibrary). ProjectA has a reference to ProjectB. Generally speaking, ProjectA calls a method in ProjectB to do some stuff and return the results to ProjectA. Sometimes however, I need ProjectB to send some “additional” information to ProjectA (more specifically to call the Console.WriteLine() method in ProjectA). To achieve this, I need to refer ProjectA in ProjectB, but when I try to do that, I get the following error:
A reference to ProjectA could not be added. Adding this project as a reference would cause a circular dependency.
I understand the whole coupling concept and it does make sense to get this message, however, I need to send additional information to ProjectA in some cases. Any ideas?
I suggest you to use events and listeners. You can, for example, send messages from ProjectB through
Trace.WriteLinewhile, in ProjectA, you would add a subscriber for the trace. .NET already offers aConsoleTraceListenerclass, to routeTracemessages to the Console. You can add a listener from ProjectA through:Alternatively, if you don’t want to use the integrated classes, you can build a very simple “source” class in ProjectB which will exposes an event with
Action<string>as its signature (although I’d suggest you to create a delegate for it), then subscribe to it from ProjectA. Generally, .NET classes are more flexible.ProjectB
ProjectA