Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Assembly added via Add Reference not copied to output directory unless referred to in code

Situation:

  • Project 1 is an assembly in the solution
  • Project 2 is an executable assembly project in the same solution
  • Project 2 has a project reference (via Add Reference) to Project 1
  • Project 2 does not directly refer to namespaces/types in Project 1 in the code
  • Project 2 uses Ninject to dynamically load Project 1 and use the implementation classes within it

Problem:

  • Even though Copy Local is set to True for the reference, and the referenced assembly does not exist in the GAC, the referenced assembly is not copied to the output build directory
  • Ninject subsequently does not find the assembly, and so the binding/resolution fails

This process works fine for an identical setup where some classes in the referenced assembly are referenced directly, as well as loaded by Ninject, so I believe the cause is as follows: If VS detects that no types within a referenced assembly are referred to in code, it won't copy the referenced assembly, even if it's added as a reference with Copy Local = True.

Solution(s):

  • Find a way of telling VS, "Copy Local (I really mean it)" = True -- this would be my ideal solution.
  • Add a file reference to the referenced assembly. Not great since I lose the advantages of a project reference (which may or may not be entirely in my head).
  • Refer to the assembly in some 'token' way in code. Messy - I'd like to use the References list to maintain a list of assemblies/modules I want to include.
  • Post-build steps to copy the assembly around. Messy, not ideal.

Can anyone help with the first solution, or suggest another?

like image 284
Kieren Johnstone Avatar asked Dec 29 '11 16:12

Kieren Johnstone


1 Answers

If you deploy/copy an application that contains a reference to a custom component that is registered in the GAC, the component will not be deployed/copied with the application, regardless of the Copy Local setting. See MSDN

You have to force copy local to true by adding Private metadata to the GAC assembly reference. Edit your project file and add Private metadata:

<Reference ..>
    <Private>True</Private>
</Reference ..>

<ProjectReference ..>
    <Private>True</Private>
</ProjectReference ..>

Now your GAC assembly should be copied/dropped from the output folder.

like image 82
Ludwo Avatar answered Oct 06 '22 00:10

Ludwo