C# 如何使用 MSBuild 引用不同版本的 dll

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1997268/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-06 22:31:09  来源:igfitidea点击:

How to reference different version of dll with MSBuild

c#visual-studiodeploymentmsbuild

提问by mickyjtwin

I have a web application project which utilises a set of 3rd party dll's. The issue is that the dev/staging environment is 32bit, but production is 64bit. As such, we have to re-reference and build the solution each time we want to deploy. I am now wanting to automate this, but unsure how to go about it with MSBuild?

我有一个 Web 应用程序项目,它使用了一组 3rd 方 dll。问题是开发/登台环境是 32 位,但生产环境是 64 位。因此,我们每次要部署时都必须重新引用和构建解决方案。我现在想自动执行此操作,但不确定如何使用 MSBuild 进行操作?

All other dll's are the same, just the 3 3rd party dll's.

所有其他 dll 都是相同的,只有 3 个 3rd 方 dll。



EDIT

编辑

I have made some headway, however am coming up with some runtime assembly issues.

我已经取得了一些进展,但是我提出了一些运行时程序集问题。

I have 3 dll files, 1.dll, 2.dll, 3.dll. The file version is 5.1 for each. For the 64 bit dlls, the names are exactly the same, just difference file versions. What I have done, is renamed each one to 1.v5.dll, 1.v6.dll etc. In my project files, I am then referencing each dll as follows:

我有 3 个 dll 文件,1.dll、2.dll、3.dll。每个文件版本为 5.1。对于 64 位 dll,名称完全相同,只是文件版本不同。我所做的是将每个 dll 重命名为 1.v5.dll、1.v6.dll 等。在我的项目文件中,我将按如下方式引用每个 dll:

<Reference Include="1.v5.dll" Condition="'$(Platform)'=='x86'">
  <SpecificVersion>False</SpecificVersion>
  <HintPath>bin.v5.dll</HintPath>
  <Private>False</Private>
</Reference>
<Reference Include="1.v6.dll" Condition="'$(Platform)'=='x64'">
  <SpecificVersion>False</SpecificVersion>
  <HintPath>bin.v6.dll</HintPath>
  <Private>False</Private>
</Reference>

This works in Visual Studio IDE, and my solution compiles file, however when I go to run the website, I get the following error...

这在 Visual Studio IDE 中有效,我的解决方案编译文件,但是当我去运行网站时,我收到以下错误...

"Could not load file or assembly '1.v5' or one of its dependencies. The located assembly's manifest definition does not match the assembly reference.

“无法加载文件或程序集‘1.v5’或其依赖项之一。定位的程序集的清单定义与程序集引用不匹配。

Any thoughts on how to approach this?

关于如何解决这个问题的任何想法?

采纳答案by mickyjtwin

This is what I have figured out, and seems to work no problems.

这是我想出来的,似乎没有问题。

I have created 2 solution platforms, x86and x64. I have created a new folder in my solution directory called "References", and created n x86 and x64 folder: \References\x86\ \References\x64\ The 3 dll's for each are then placed in their respective directories.

我创建了 2 个解决方案平台,x86x64。我在我的解决方案目录中创建了一个名为“References”的新文件夹,并创建了 n 个 x86 和 x64 文件夹: \References\x86\ \References\x64\ 然后将每个 3 个 dll 放在各自的目录中。

In each project's file, I have then added the following references:

在每个项目的文件中,我添加了以下引用:

<Reference Include="{Reference1}" Condition="'$(Platform)'=='x86'">
  <HintPath>..\References\dlls\x86\{Reference1}.dll</HintPath>
</Reference>
<Reference Include="{Reference2}" Condition="'$(Platform)'=='x64'">
  <HintPath>..\References\dlls\x64\{Reference2}.dll</HintPath>
</Reference>

Now, when I develop within the IDE, I am working the the relevant dll specific to my needs.

现在,当我在 IDE 中开发时,我正在处理特定于我的需求的相关 dll。

I have then just added a post-build event which copies the dll based on the $(Platform) variable into the bin directory.

然后我刚刚添加了一个构建后事件,它将基于 $(Platform) 变量的 dll 复制到 bin 目录中。

回答by stijn

You can create conditional references in the project file like this:

您可以在项目文件中创建条件引用,如下所示:

<Reference Include="32bit.dll" Condition="'$(Platform)'=='x86'"/>
<Reference Include="64bit.dll" Condition="'$(Platform)'=='x64'"/>

To use this inside VS, you have to create two solution platforms: one for the x86 target and one for the x64 target. Depending on the active platform one of the dlls will be selected, no need for re-referencing.

要在 VS 中使用它,您必须创建两种解决方案平台:一种用于 x86 目标,一种用于 x64 目标。根据活动平台,将选择 dll 之一,无需重新引用。

To automate this using msbuild, create a new project file that builds the other project file a number of times, each time for a different platform/configuration/...:

要使用 msbuild 自动执行此操作,请创建一个新项目文件,该文件多次构建另一个项目文件,每次针对不同的平台/配置/...:

<Target Name="BuildAll">
  <MSBuild Targets="myproject.proj" Properties="Platform=x86;Configuration=Debug"/>
  <MSBuild Targets="myproject.proj" Properties="Platform=x64;Configuration=Debug"/>
  <MSBuild Targets="myproject.proj" Properties="Platform=x64;Configuration=Release"/>
</Target>

Have a look at the MSBuild task reference for aditional options like building in parallel.

查看 MSBuild 任务参考以了解其他选项,例如并行构建。