.Net编译时出现无法解决"xxx"之间的冲突

日期 2019-02-25 默认分类,服务器,C# 作者 叫我蓝火火 共0评论

今天遇到一个非常风骚的问题。
我们老老实实的编译asp.net mvc项目,只是从nuget引入了阿里的两个sdk包,没有改任何配置的前提下,生成项目会报如下警告:

6>C:\Program Files (x86)\Microsoft Visual Studio\2017\Enterprise\MSBuild\15.0\Bin\Microsoft.Common.CurrentVersion.targets(2110,5): warning MSB3243: 无法解决“System.Net.Http, Version=4.2.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a”与“System.Net.Http, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a”之间的冲突。正在随意选择“System.Net.Http, Version=4.2.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a”。
6>  请考虑使用 app.config 将程序集“System.Net.Http, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a”从版本“4.0.0.0”[C:\Program Files (x86)\Reference Assemblies\Microsoft\Framework\.NETFramework\v4.7.1\System.Net.Http.dll]重新映射到版本“4.2.0.0”[C:\Program Files (x86)\Microsoft Visual Studio\2017\Enterprise\MSBuild\Microsoft\Microsoft.NET.Build.Extensions\net471\lib\System.Net.Http.dll],以解决冲突并消除警告。

作为一个非专业的程序员,对warning还做不到视而不见,于是着手解决这个问题。
然后随手必应一下,随手贴上了一段


<dependentAssembly>
  <assemblyIdentity name="System.Net.Http" publicKeyToken="b03f5f7f11d50a3a" culture="neutral" />
  <bindingRedirect oldVersion="0.0.0.0-4.2.0.0" newVersion="4.0.0.0" />
</dependentAssembly>

小手一敲,依旧警告。
难道文档是错的?但是看其他人的文章,都说这样可以解决,难道我用了个假的.net framework?
经过一顿操作,输出改成详细模式,终于找到了一点点蛛丝马迹:这个4.2.0.0版本的dll存在一个叫Microsoft.NET.Build.Extensions的文件夹,而编译的时候仿佛引入了Microsoft.NET.Build.Extensions文件夹里的一个targets文件。

3>来自项目“xxx”的文件“C:\Program Files (x86)\Microsoft Visual Studio\2017\Enterprise\MSBuild\Microsoft\Microsoft.NET.Build.Extensions\Microsoft.NET.Build.Extensions.NETFramework.targets”中的目标“ImplicitlyExpandNETStandardFacades”(目标“ResolveAssemblyReferences”依赖于它):
3>正在使用程序集“C:\Program Files (x86)\Microsoft Visual Studio\2017\Enterprise\MSBuild\Microsoft\Microsoft.NET.Build.Extensions\\tools\net46\Microsoft.NET.Build.Extensions.Tasks.dll”中的“GetDependsOnNETStandard”任务。
3>任务“GetDependsOnNETStandard”
3>已完成执行任务“GetDependsOnNETStandard”的操作。

看起来问题解决了一部分,项目自动引入了一个.net standard的类库,导致冲突。
但是我也没引入这东西啊,csproj里也没配置ImplicitlyExpandNETStandardFacades相关的东西,所以打开targets看下里面是什么东西:


<PropertyGroup>
  <MSBuildAllProjects>$(MSBuildAllProjects);$(MSBuildThisFileFullPath)</MSBuildAllProjects>

  <!-- Add NETStandard references if targetframeworkversion supports netstandard2.0 (net461 or later) -->
  <ImplicitlyExpandNETStandardFacades Condition="'$(ImplicitlyExpandNETStandardFacades)' == '' AND '$(_TargetFrameworkVersionWithoutV)' &gt;= '4.6.1'">true</ImplicitlyExpandNETStandardFacades>
</PropertyGroup>

<!-- ... -->

    <!--
      .NET 4.7.1 has support for .NET Standard 2.0 built-in, so most of the facades aren't necessary.  However, the assembly versions of a few set of assemblies
      do not yet match the ones shipped in the support package for .NET Standard 2.0 on .NET 4.7 and below. This means that .NET 4.7 or lower libraries might have
      references to higher versions of these assemblies than are available in-box in .NET 4.7, leading to assembly loading errors. So if there is a dependency on
      netstandard.dll, we include DLLs for .NET 4.7.1 from the support package to avoid these errors.
      -->
<ItemGroup Condition="'$(_TargetFrameworkVersionWithoutV)' == '4.7.1'">
  <_NETStandardLibraryNETFrameworkLib Include="$(MSBuildThisFileDirectory)\net471\lib\*.dll" Condition="'$(DependsOnNETStandard)' == 'true'"/>
</ItemGroup>

你妈的,神经病啊。在项目文件csproj的第一个PropertyGroup中添加

<ImplicitlyExpandNETStandardFacades>false</ImplicitlyExpandNETStandardFacades>

编译通过,运行正常,以后出问题再说。
这个问题应该只会出现在现在这个Framework到core的过渡期,先凑合着来吧。

如果有错误请告诉我,谢谢各位大佬