使用 csproj 覆盖构建输出中的 dll

overwrite dll in build output using csproj

有一个 known issue 和 System.Net.Http。

Visual Studio 2017 将从 C:\Program Files (x86)\Microsoft Visual Studio17\Professional\MSBuild\Microsoft\Microsoft.NET.Build.Extensions\net461\lib 中获取 System.Net.Http.dll 并将其放入构建输出中。在我的机器上,这是版本 4.3.4(文件版本 4.6.26011.1)

如果您使用 <PackageReference> 甚至直接 <Reference> 到不同版本的 System.Net.Http.dll,Visual Studio 2017 将忽略您的版本而支持其版本创建构建输出(在 /bin 文件夹中)。

当您尝试从外部调用 dll 时,这将导致错误。

主要问题:

有什么方法可以强制 msbuild/csproj 使用我从 NuGet 引用的版本吗? 也许,我可以在 post-build 操作中复制并覆盖现有的 System.Net.Http.dll 吗?

我试过的:

我尝试使用绑定重定向,但这似乎根本不会影响构建输出 (see this comment)。

可能有更好的方法,但这对我有用:

  1. 找到System.Net.Http.dll的正确版本然后放置 您的项目可以访问的某个地方(例如在 /libs 文件夹中 你的根项目文件夹)
  2. 使用 msbuild/csproj post-构建操作,将正确的版本复制到构建输出

.csproj 步骤 2 代码段:

<Target Name="CopyFiles" AfterTargets="Build">
  <Copy SourceFiles="libs\System.Net.Http.dll" DestinationFolder="$(OutputPath)" />
</Target>

注:

据推测,this will be fixed in net472