2

I have a solution in which I needed to brand an assembly for a third-party client. This resulted in a class structure as follows:

Project1
  References
    MyBrandedAssembly.dll (namespace: MyAssembly)
Project2
  References
    Project1

When MsBuild built Project1, it could resolve the MyAssembly namespace to MyBrandedAssembly.dll:

Primary reference "MyAssembly".
  Resolved file path is "SolutionPath\Binaries\MyBrandedAssembly.dll".
  Reference found at search path location "{HintPathFromItem}".

But when building Project2, it could not resolve the second-order reference:

Dependency "MyAssembly".
  Could not resolve this reference. Could not locate the assembly "MyAssembly". 
   Check to make sure the assembly exists on disk. If this reference is required by your  
   code, you may get compilation errors.
    For SearchPath "SolutionPath\Project1\bin\Debug".
    Considered "SolutionPath\Project1\bin\Debug\MyAssembly", but it didn't exist.

Why not? And how can I force it to?

Arithmomaniac
  • 3,978
  • 2
  • 31
  • 52
  • 1
    Can you add a reference to MyBrandedAssembly.dll in Project2? – Steve Wong Feb 29 '16 at 19:39
  • I could, but is there a more "automatic" way? – Arithmomaniac Feb 29 '16 at 19:46
  • This thread has an alternate solution that could work for you: http://stackoverflow.com/questions/1132243/msbuild-doesnt-copy-references-dlls-if-using-project-dependencies-in-solution?rq=1 – Steve Wong Feb 29 '16 at 19:49
  • @SteveWong: Are you thinking of the top solution? That requires both a reference *and* code; it's even less automatic – Arithmomaniac Feb 29 '16 at 20:47
  • as I understood the accepted answer, you have 2 options: 1) add a reference to the dependent assembly (in Project2) OR 2) reference a type of the dependent assembly from your Project1 code. – Steve Wong Feb 29 '16 at 20:54

1 Answers1

1

You can add a pre-build event to your project csproj file to copy the dlls from the Project 1 output bin folder to the Project 2 bin folder. This will allow the assembly to be found by the assembly resolver.

Add this to your project2.csproj

<Target Name="BeforeBuild">
    <Delete Files="$(ProjectDir)bin\$(Configuration)\MyBrandedAssembly.dll" />
    <Copy SourceFiles="$(ProjectDir)..\<Project1>\bin\$(Configuration)\MyBrandedAssembly.dll" DestinationFolder="$(ProjectDir)bin\$(Configuration)\" />
</Target>
Letseatlunch
  • 2,221
  • 4
  • 26
  • 32
  • Also if you have a list of dlls you need to copy you can create an item group and reference that in the SourceFiles. Take a look at https://msdn.microsoft.com/en-us/library/3e54c37h.aspx for additional parameters and an example using a list of files – Letseatlunch Feb 29 '16 at 22:20