Microsoft.Net.Http MVC 项目和 PCL 之间的库冲突

Microsoft.Net.Http library conflict between MVC project and PCL

我正在为我的 WebApp 和 PCL.

使用 Microsoft HTTP 客户端库 2.2.29

这里是link:https://www.nuget.org/packages/Microsoft.Net.Http/

PCL 正在使用 System.Net.Http.Primitives.dll 版本 1.7.29 因为兼容性。

MVC 项目正在拉 System.Net.Http.Primitives.dll 版本 4.2.29 因为它与 4.5 框架兼容。它也依赖于我的 PCL.

如果我只构建我的 WebApp,它工作正常,因为它提取 4.2.29 版本。但是,如果我构建另一个依赖于我的 PCL 的项目(例如 Xamarin APP),它会将 1.7.29 版本放入 web/bin 文件夹中,并且我在 Web 项目上收到以下错误。

[FileLoadException: Could not load file or assembly 'System.Net.Http.Primitives, Version=4.2.29.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a' or one of its dependencies. The located assembly's manifest definition does not match the assembly reference. (Exception from HRESULT: 0x80131040)]
System.Reflection.RuntimeAssembly._nLoad(AssemblyName fileName, String codeBase, Evidence assemblySecurity, RuntimeAssembly locationHint, StackCrawlMark& stackMark, IntPtr pPrivHostBinder, Boolean throwOnFileNotFound, Boolean forIntrospection, Boolean suppressSecurityChecks) +0
System.Reflection.RuntimeAssembly.InternalLoadAssemblyName(AssemblyName assemblyRef, Evidence assemblySecurity, RuntimeAssembly reqAssembly, StackCrawlMark& stackMark, IntPtr pPrivHostBinder, Boolean throwOnFileNotFound, Boolean forIntrospection, Boolean suppressSecurityChecks) +234
System.Reflection.RuntimeAssembly.InternalLoad(String assemblyString, Evidence assemblySecurity, StackCrawlMark& stackMark, IntPtr pPrivHostBinder, Boolean forIntrospection) +108
System.Reflection.RuntimeAssembly.InternalLoad(String assemblyString, Evidence assemblySecurity, StackCrawlMark& stackMark, Boolean forIntrospection) +25 System.Reflection.Assembly.Load(String assemblyString) +34
System.Web.Configuration.CompilationSection.LoadAssemblyHelper(String assemblyName, Boolean starDirective) +49

我的web.config配置如下:

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

PCL packages.config

<package id="Microsoft.Net.Http" version="2.2.29" targetFramework="portable45-net45+win8+wp8+wpa81" />

WebAPPpackages.config

 <package id="Microsoft.Net.Http" version="2.2.29" targetFramework="net451" />

我可以删除 web.config 配置以使其与版本 1.7.29 一起使用。但是我不想这样做,我想在每个项目中使用这个库的最新兼容版本。

为什么 VS 2015 社区 edition/nuget 会在我构建不相关的项目时将 non-compatible/lower 版本的库放入 web/bin 文件夹?

我该如何解决这个问题?

我知道这不是一个经过验证的解决方案,但是,我不得不从解决方案中一个一个地删除项目并重新添加。然后我通过右键单击项目并选择对它们的依赖关系来重新映射项目依赖关系。现在好像在工作。当超过 3 个项目相互交叉引用时,VS 构建顺序可能有问题。