Microsoft.Azure.ActiveDirectory.GraphClient 需要不正确的依赖项版本

Microsoft.Azure.ActiveDirectory.GraphClient requires incorrect version of dependencies

在Asp.Net针对4.6.1完整框架的核心解决方案我使用的是2.1.1Microsoft.Azure.ActiveDirectory.GraphClient.dll

在 MSTest 测试库 TestMethods 中出现异常:

System.IO.FileLoadException: Could not load file or assembly 'Microsoft.Data.Services.Client, Version=5.6.3.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35' or one of its dependencies. The located assembly's manifest definition does not match the assembly reference. (Exception from HRESULT: 0x80131040)

奇怪的是,nuspec 需要 5.6.4 版本,但 DLL 实际上引用了 5.6.3。

从 dotPeek 中提取:

// Assembly Microsoft.Azure.ActiveDirectory.GraphClient, Version=2.1.10.0, Culture=neutral, PublicKeyToken=null
// Assembly references:
// Microsoft.Data.Services.Client, Version=5.6.3.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35
// Microsoft.Data.Edm, Version=5.6.3.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35
// Microsoft.Data.OData, Version=5.6.3.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35

.nuget\packages\Microsoft.Azure.ActiveDirectory.GraphClient.1.1\Microsoft.Azure.ActiveDirectory.GraphClient.nuspec

 <dependencies>
      <dependency id="Microsoft.Data.Services.Client" version="5.6.4" />
      <dependency id="Microsoft.Data.Edm" version="5.6.4" />
      <dependency id="Microsoft.Data.OData" version="5.6.4" />
      <dependency id="System.Spatial" version="5.6.4" />
    </dependencies>

我尝试安装 Microsoft.Data.Services.Client 5.6.3,但 NuGet 报告说 Microsoft.Data.Odata 需要 5.6.4
我尝试安装 Microsoft.Data.Odata 5.6.3,但 NuGet 报告 Microsoft.Azure.ActiveDirectory.GraphClient 需要 5.6.4

我尝试使用 assemblyBinding,但它对我不起作用(我尝试了 assemblybinding does not work in mstest 的建议)

有什么关于如何加载 Microsoft.Data.Services.Client 的建议吗?
我能以某种方式覆盖 nuspec 依赖项吗?

更新:我创建了 isolated library with single TestMethod,但有问题。

修复: 问题是在 assemblyBinding/bindingRedirect 的 app.config 中我使用了文件版本而不是程序集版本,这对于这个程序集是不同的 (文件版本=5.6.4.62175,但程序集版本=5.6.4.0)。正确的配置是

 <assemblyBinding xmlns="urn:schemas-microsoft-com:asm.v1">
      <dependentAssembly>
        <assemblyIdentity name="Microsoft.Data.Services.Client" publicKeyToken="31bf3856ad364e35" culture="neutral" />
        <bindingRedirect oldVersion="0.0.0.0-5.6.4.0" newVersion="5.6.4.0" />
      </dependentAssembly>
    </assemblyBinding>

我正在使用 Visual Studio 2017 进行测试,并安装版本为 2.1.1 的软件包 Microsoft.Azure.ActiveDirectory.GraphClient。我还检查 Microsoft.Data.Services.Client 的依赖版本是你提到的 5.6.3 版本。

但是,我也检查了 bin 文件夹中 Microsoft.Data.Services.Client.dll 的版本是 5.6.4.0,它对我来说很好用。请使用此版本,看看是否有帮助。

这里是 deps.json 文件的一部分供您参考:

 {
      "runtimeTarget": {
        "name": ".NETFramework,Version=v4.6.1/win7-x86",
        "signature": "d7dc1119a66d1c2bf3e9f2c55e2b0432742f00d1"
      },
      "compilationOptions": {
        "defines": [
          "TRACE",
          "DEBUG",
          "NET461"
        ],
        "languageVersion": "",
        "platform": "x86",
        "allowUnsafe": false,
        "warningsAsErrors": false,
        "optimize": false,
        "keyFile": "",
        "emitEntryPoint": true,
        "xmlDoc": false,
        "debugType": "portable"
      },
      "targets": {
        ".NETFramework,Version=v4.6.1": {
          "consoleapp1/1.0.0": {
            "dependencies": {
              "Microsoft.AspNetCore.Server.Kestrel": "1.0.3",
              "Microsoft.Azure.ActiveDirectory.GraphClient": "2.1.1"
            },
            "compile": {
              "ConsoleApp1.exe": {}
            }
          },
          ...
          "microsoft.azure.activedirectory.graphclient/2.1.1": {
            "dependencies": {
              "Microsoft.Data.Edm": "5.6.4",
              "Microsoft.Data.OData": "5.6.4",
              "Microsoft.Data.Services.Client": "5.6.4",
              "System.Spatial": "5.6.4"
            },
            "compile": {
              "lib/portable-net4+sl5+win+wpa+wp8/Microsoft.Azure.ActiveDirectory.GraphClient.dll": {}
            }
          },
          "microsoft.data.edm/5.6.4": {
            "compile": {
              "lib/net40/Microsoft.Data.Edm.dll": {}
            }
          },
          "microsoft.data.odata/5.6.4": {
            "dependencies": {
              "Microsoft.Data.Edm": "5.6.4",
              "System.Spatial": "5.6.4"
            },
            "compile": {
              "lib/net40/Microsoft.Data.OData.dll": {}
            }
          },
          "microsoft.data.services.client/5.6.4": {
            "dependencies": {
              "Microsoft.Data.OData": "5.6.4"
            },
            "compile": {
              "lib/net40/Microsoft.Data.Services.Client.dll": {}
            }
          },
         ...
        }
      }
    }

更新

对于单元测试项目中的重定向程序集,添加如下配置:

1 .添加配置文件testAssemblyName.dll.config(例如,我的项目名称是ConsoleApp1.Test 那么配置文件应该是ConsoleApp1.Test.dll.config),内容如下:

<?xml version="1.0" encoding="utf-8"?>
<configuration>
  <runtime>
    <assemblyBinding xmlns="urn:schemas-microsoft-com:asm.v1">
      <dependentAssembly>
        <assemblyIdentity name="Microsoft.Data.Services.Client" publicKeyToken="31bf3856ad364e35" culture="neutral" />
        <bindingRedirect oldVersion="0.0.0.0-5.6.4.0" newVersion="5.6.4.0" />
      </dependentAssembly>
    </assemblyBinding>
    <assemblyBinding xmlns="urn:schemas-microsoft-com:asm.v1">
      <dependentAssembly>
        <assemblyIdentity name="Microsoft.Data.OData" publicKeyToken="31bf3856ad364e35" culture="neutral" />
        <bindingRedirect oldVersion="0.0.0.0-5.6.4.0" newVersion="5.6.4.0" />
      </dependentAssembly>
    </assemblyBinding>
    <assemblyBinding xmlns="urn:schemas-microsoft-com:asm.v1">
      <dependentAssembly>
        <assemblyIdentity name="Microsoft.Data.Edm" publicKeyToken="31bf3856ad364e35" culture="neutral" />
        <bindingRedirect oldVersion="0.0.0.0-5.6.4.0" newVersion="5.6.4.0" />
      </dependentAssembly>
    </assemblyBinding>
  </runtime>
</configuration>
  1. 将 属性 Copy to Output Directory 更改为值 Copy if newer 以确保将配置复制到 bin/debug文件夹。

之后测试 运行 成功。