.NET中的库版本冲突:System.Net.Http

时间:2017-11-04 02:42:26

标签: .net nuget

我正在尝试使用NuGet Package Manager控制台将更改部署到数据库。当我发出Update-Database命令时,它失败并显示错误消息:

Could not load file or assembly 'System.Net.Http, Version=4.1.1.1,
  Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a' or one of its
  dependencies. The system cannot find the file specified.

我的项目针对的是.NET 4.5.2,据我所知,版本System.Net.Http中提供了4.0.0.0。因此,被控制的库不会放在二进制输出目录中。当我下载4.1.1.1版本(NuGet包版本4.3.2)并将其复制到二进制输出目录以及我的DLL时,我收到另一条错误消息:

Loading this assembly would produce a different grant set from other
  instances. (Exception from HRESULT: 0x80131401)

在这两种情况下,异常都是System.IO.FileLoadException,它会在Microsoft.Rest.ServiceClient'1.CreateRootHandler()中抛出。我检查了该函数的源代码,它实际上引用了.NET 4.5和System.Net.Http

  protected static HttpClientHandler CreateRootHandler()
  {
    // Create our root handler
#if NET45
    return new WebRequestHandler();
#else
    return new HttpClientHandler();
#endif
  }

但是,我无处可找到System.Net.Http的确切版本号。最后,我使用ILSpy来检查Microsoft.Rest.ClientRuntime,它似乎依赖于版本System.Net.Http中的4.0.0.0

ILSpy inspection into Microsoft.Rest.ClientRuntime

我还尝试删除对System.Net.Http的所有引用并安装最新的NuGet(4.3.2)。这导致后续错误的方法丢失:

Method not found: 'Void Microsoft.Azure.KeyVault.KeyVaultClient..ctor(
  AuthenticationCallback, System.Net.Http.DelegatingHandler[])'.

Method not found: 'Void Microsoft.Rest.ServiceClient`1..ctor(
  System.Net.Http.DelegatingHandler[])'.

我通过将相应的DLL从相应的NuGet复制到部署目录中来解决。这最终导致:

Loading this assembly would produce a different grant set from other
  instances. (Exception from HRESULT: 0x80131401)

我错过了什么?

1 个答案:

答案 0 :(得分:1)

在找到Kunal的答案之后:

MongoDB client throws a FileNotFoundException in mscorlib

为我工作,我尝试更改System.Net.Http的版本号。我安装了面向net462的System.Net.Http版本4.3.3,这对我有用:

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