Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

MSBuild error: "Could not resolve this reference. Could not locate the assembly..."

I am aware that there is already a question here asking about the exact same error message, but unfortunately the accepted answer doesn't work for me.

I was able to build my current solution successfully earlier this morning, but the build suddenly started failing ~15 minutes ago, with the following error message:

C:\Program Files (x86)\MSBuild\14.0\bin\Microsoft.Common.CurrentVersion.targets(1820,5): warning MSB3245: Could not resolve this reference. Could not locate the assembly "MyAssembly.dll". Check to make sure the assembly exists on disk. If this reference is required by your code, you may get compilation errors.

Unfortunately for me, this DLL is required by my code. Because it cannot be located, my solution won't compile.

I have tried cleaning and then building my solution again, but that was useless.

I noticed that there was a yellow icon next to this reference, so I removed the reference and then added it again from the exact same location specified inside the <HintPath> tag in my .csproj file. (The location was the \bin\Debug folder.) The yellow icon then disappeared.

However, immediately after I clicked on "Build solution" (in Debug mode), the yellow icon appeared again, and once more I saw the same MSBuild error message, informing me that the DLL could not be located.

How can I resolve this issue?

like image 945
M.Y. Babt Avatar asked Dec 24 '22 12:12

M.Y. Babt


1 Answers

Add a project reference to the MyAssembly project, not a reference to the compiled output. Right click on the References node of the project which needs MyAssembly.dll, go to "Add Reference", then choose "Solution":

Solution reference


Say you have two projects (X and Y) with Y depending on X. Visual Studio will build X first, then Y.

You can add a reference to X in Y in one of two ways:

  • Reference the project in the solution
  • Reference the compiled output in the \bin\Debug folder

If the reference is to the project in the solution, then Visual Studio will know which order to compile your projects (X, then Y in our example).

If the reference is to the output in \bin\Debug, then Visual Studio won't know which order to compile the assemblies, and could try to compile Y before X, not find the file in \bin\Debug, and not compile.

like image 105
Wai Ha Lee Avatar answered Jan 01 '23 17:01

Wai Ha Lee