31

Let's say we have a solution with the following structure:

  • Project.DAL - Data access layer, depends on a lower-level library, e.g. Oracle.DataAccess w/copy local = true
  • Project.BLL - Business logic layer, references Project.DAL as project
  • Project.UI - UI layer, compiles to executable, references Project.BLL, default project

When Project.UI is compiled, VS is smart enough to copy Project.DAL.dll to the output directory, but it's not smart enough to figure out that I wanted Oracle.DataAccess to be copied to the output directory as well for distribution to clients.

Can anyone explain why this is so? Is it because it sees Oracle.DataAccess in the GAC and assumes that clients will have it in the GAC as well?

It's not that big of a deal, but it's kinda annoying that every time I add a new assembly reference, I have to remember to set it to copy local and add an item to copy it in my build script as well.

4 Answers 4

31

One more thing.

When you don't use the referenced DLL in your code at all, it will ignore the CopyLocal and won't copy it to your output directory.

3
  • 19
    Which makes things annoying when you're using Dependency Injection and you're not directly referencing any specific implementation. Weeee!
    – kdawg
    Commented Apr 22, 2011 at 16:03
  • @SimonKarlsson Yes, but it's a bit dirty, see this answers: stackoverflow.com/a/24828522/249948 and stackoverflow.com/a/21055664/249948. I'd like to see a better solution as well. Commented Jan 6, 2016 at 8:05
  • @hermancordes Thanks for your reply! This is unfortunately how I ended up solving it. Commented Jan 7, 2016 at 8:01
27

Yes, Visual Studio will copy a DLL to the output path in any of the two conditions below:

  1. The DLL is referenced explicitly with CopyLocal = true
  2. The DLL is referenced without CopyLocal or implicitly through some other referenced DLL and is not in the GAC

The reason why it will not copy-local when the file is in GAC, is that when resolving assembly names the GAC has highest priority, i.e. even if you have a (different) local copy, the version from the GAC will be used.

I suggest you set up a library directory where you put all external assemblies that are referenced. Then you set up an automatic MSBuild script on a computer (or VM) that does not have the Oracle-file gac'ed (nor Visual Studio installed for that sake). That way, the file will be copied to the build, and you will have more control over what is done than when using VS.

4
  • 3
    +1 for highlighting "and is NOT in the GAC"... this caused me a day of problems. Commented Aug 2, 2012 at 15:09
  • Beware that the default value of CopyLocal (which is used if CopyLocal ist not explicitly specified in the project file) was recently changed in VS 2015 (connect.microsoft.com)!
    – Andreas
    Commented Aug 3, 2016 at 14:51
  • How I get a,e.g. xsd file copy to output folder? I cannot reference a xsd file directly in the code.
    – Johannes
    Commented Aug 31, 2016 at 11:11
  • "Visual Studio will copy a DLL to the output path in any of the two conditions below" are you talking about the output path of each project or the output path of the main project (e.g. Project.UI)? When you set CopyLocal=true the DLL is always copied to the output path (even if it exists in the CAG), but it won't be copied to the output path of projects that reference this one. You also state that "DLL is referenced without CopyLocal (do you mean CopyLocal=false?) and is not in the GAC" will be copied to the output path, but the DLL doesn't show up in the output path if CopyLocal=false. Commented Nov 13, 2020 at 12:37
17

I had a strange situation where even though the assembly was a project reference and was referenced with "Copy Local" showing up as "True" in the reference properties window, the DLL was not being copied to the output directory. I had an earlier version of the DLL in the GAC but I didn't see why this should prevent the DLL being copied.

I found that by unloading the project and manually editing the project reference XML as follows:

<ProjectReference Include="..\SomeProject.csproj">
  <Project>{11111111-1111-1111-1111-111111111111}</Project>
  <Name>Some Project Name</Name>
  <Private>True</Private>
</ProjectReference>

The DLL was copied to the ouput directory as expected. I found that just setting Copy Local to True in the properties window meant the <Private> element was completely missing, but in the case of it being set to false it was present with a value of "False".

1
  • 7
    An alternative to manually editing the .csproj file it to set Copy Local to False, do a File - Save All, then set Copy Local to True and rebuild the project.
    – RenniePet
    Commented Sep 20, 2013 at 12:47
3

@RenniePet Here's a link to a blog which describes the method RenniePet described in a comment above (if you don't want to edit your project file manually as @Shaun suggested):

http://blogs.msdn.com/b/jjameson/archive/2009/11/18/the-copy-local-bug-in-visual-studio.aspx

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.