How to get Visual Studio to stop copying DLLs during build without my permission?
Asked Answered
M

4

8

I have a Visual Studio project that relies on several DLL references. Here is a sample of those references in my csproj:

<ItemGroup>
  <Reference Include="Class1.Project1">
    <SpecificVersion>False</SpecificVersion>
    <HintPath>..\bin\Class1.Project1.dll</HintPath>
    <Private>False</Private>
  </Reference>
  <Reference Include="Class1.Project2">
    <SpecificVersion>False</SpecificVersion>
    <HintPath>..\bin\Class1.Project2.dll</HintPath>
    <Private>False</Private>
  </Reference>
</ItemGroup>

However, when I include this class as a project dependency in a web site project, Visual Studio is finding dependencies of the dependencies shown above. During build Visual Studio is then defaulting the "Copy Local" property to "True" and copying these dependencies into my web site's ~/bin directory.

This, in turn, is overwriting the versions of the DLL files that already exist in this directory. This causes the following error:

Could not load file or assembly 'Class5.Project5, Version=3.6.1861.2, Culture=neutral, PublicKeyToken=dfeaee0e3978ac79' or one of its dependencies. The located assembly's manifest definition does not match the assembly reference. (Exception from HRESULT: 0x80131040)

How do I make Visual Studio default the "Copy Local" setting to "False" for everything? I do not want Visual Studio to copy DLL files automatically during build. Nor do I want to tie my build to very specific versions of a DLL.

Mate answered 13/5, 2009 at 19:51 Comment(0)
U
2

It sounds to me as though you have multiple projects configured to output into the same directory - is this true?

If so, you'll need to review your configuration as Visual Studio assumes (nay, requires) that each project has a unique output directory.

Also, you wrote:

This, in turn, is overwriting the versions of the DLL files that already exist in this directory.

Where did these existing files come from?

Visual Studio assumes that it has full rights to make whatever changes it sees fit in the build output directories - trying to argue with it is a fine route to a whole new world of pain.

(Unfortunately, I speak from experience. Sigh.)

Upi answered 18/5, 2009 at 9:21 Comment(2)
VS does not require each project to have a unique output directory. Where I work we have over 1,000 projects that output everything toa single bin directory. (The trick is to create your references from this output directory.) We use nant to both populate the build prerequisites and clean the output directory between debug and release builds. It's probably not a best practice, but it certainly works.Aleydis
Very Interesting! When I tried this, with VS2003, I found that compilation failed because files in the output directory were in use, held open by one project when another was trying to clear things out. Which version of Visual Studio are you using?Upi
B
1

I had this problem once,

On Publish: The easiest way to prevent writing over the existing dll files is to set them as ReadOnly. You will get a warning on publish for each file that could not be replaced but it will do the job.

On Build: To set the CopyLocal automatically off you need to place the dll files on the GAC.

Balancer answered 15/5, 2009 at 11:40 Comment(0)
B
0

Why were there other versions already in the bin directory?

In any case, I wonder if you would get the same problem using a Web Application Project. Since it's a project, it has a single file listing the direct references, and if these are project references (references to the output of other assemblies in the same solution), then MSBUILD can ensure that the correct version is used.

See if you can reproduce this by starting with a new web application project and just adding the references.

Bluish answered 15/5, 2009 at 11:58 Comment(0)
R
0

You could try the following in your project file.

<ReferenceOutputAssembly>false</ReferenceOutputAssembly> 

And then, in your code try this.

<ItemGroup>
  <Reference Include="Class1.Project1">
    <SpecificVersion>False</SpecificVersion>
    <HintPath>..\bin\Class1.Project1.dll</HintPath>
    <ReferenceOutputAssembly>false</ReferenceOutputAssembly>
  </Reference>
  <Reference Include="Class1.Project2">
    <SpecificVersion>False</SpecificVersion>
    <HintPath>..\bin\Class1.Project2.dll</HintPath>
    <ReferenceOutputAssembly>false</ReferenceOutputAssembly>
  </Reference>
</ItemGroup>
Refutative answered 3/8, 2012 at 16:45 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.