如何在未经我许可的情况下让Visual Studio在构建期间停止复制DLL?

时间:2022-01-11 07:00:59

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

我有一个依赖于几个DLL引用的Visual Studio项目。以下是我的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.

但是,当我将此类作为项目依赖项包含在网站项目中时,Visual Studio将查找上面显示的依赖项的依赖项。在构建期间,Visual Studio将“复制本地”属性默认为“True”,并将这些依赖项复制到我的网站的〜/ bin目录中。

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

反过来,这会覆盖此目录中已存在的DLL文件的版本。这会导致以下错误:

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)

无法加载文件或程序集'Class5.Project5,Version = 3.6.1861.2,Culture = neutral,PublicKeyToken = dfeaee0e3978ac79'或其依赖项之一。定位的程序集的清单定义与程序集引用不匹配。 (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.

如何将Visual Studio默认为“复制本地”设置为“假”为所有内容?我不希望Visual Studio在构建期间自动复制DLL文件。我也不想将我的构建绑定到DLL的特定版本。

4 个解决方案

#1


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.

如果是这样,您将需要检查您的配置,因为Visual Studio假定(不需要)每个项目都有一个唯一的输出目录。

Also, you wrote:

另外,你写道:

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

反过来,这会覆盖此目录中已存在的DLL文件的版本。

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.

Visual Studio假定它拥有完全的权限,可以在构建输出目录中进行它认为合适的任何更改 - 尝试与它争论是一个通向全新痛苦世界的良好途径。

(Unfortunately, I speak from experience. Sigh.)

(不幸的是,我是从经验中说话。叹气。)

#2


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 Publish:防止写入现有dll文件的最简单方法是将它们设置为ReadOnly。您将收到有关每个文件的发布警告,这些文件无法替换,但它将完成工作。

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

在构建上:要自动关闭CopyLocal,您需要将dll文件放在GAC上。

#3


Why were there other versions already in the bin directory?

为什么bin目录中已经有其他版本?

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.

无论如何,我想知道你是否会在使用Web应用程序项目时遇到同样的问题。由于它是一个项目,它有一个列出直接引用的文件,如果它们是项目引用(对同一解决方案中其他程序集的输出的引用),那么MSBUILD可以确保使用正确的版本。

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

看看您是否可以通过启动新的Web应用程序项目并仅添加引用来重现此问题。

#4


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>

#1


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.

如果是这样,您将需要检查您的配置,因为Visual Studio假定(不需要)每个项目都有一个唯一的输出目录。

Also, you wrote:

另外,你写道:

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

反过来,这会覆盖此目录中已存在的DLL文件的版本。

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.

Visual Studio假定它拥有完全的权限,可以在构建输出目录中进行它认为合适的任何更改 - 尝试与它争论是一个通向全新痛苦世界的良好途径。

(Unfortunately, I speak from experience. Sigh.)

(不幸的是,我是从经验中说话。叹气。)

#2


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 Publish:防止写入现有dll文件的最简单方法是将它们设置为ReadOnly。您将收到有关每个文件的发布警告,这些文件无法替换,但它将完成工作。

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

在构建上:要自动关闭CopyLocal,您需要将dll文件放在GAC上。

#3


Why were there other versions already in the bin directory?

为什么bin目录中已经有其他版本?

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.

无论如何,我想知道你是否会在使用Web应用程序项目时遇到同样的问题。由于它是一个项目,它有一个列出直接引用的文件,如果它们是项目引用(对同一解决方案中其他程序集的输出的引用),那么MSBUILD可以确保使用正确的版本。

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

看看您是否可以通过启动新的Web应用程序项目并仅添加引用来重现此问题。

#4


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>