在针对4.6.1完整框架的Asp.Net Core解决方案中我使用2.1.1 Microsoft.Azure.ActiveDirectory.GraphClient.dll
在MSTest测试库TestMethods中我遇到异常:
System.IO.FileLoadException:无法加载文件或程序集 'Microsoft.Data.Services.Client,Version = 5.6.3.0,Culture = neutral, PublicKeyToken = 31bf3856ad364e35'或其依赖项之一。该 找到程序集的清单定义与程序集不匹配 参考。 (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 \包\ Microsoft.Azure.ActiveDirectory.GraphClient \ 2.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,这有问题。
FIX: 问题是在assemblyBinding / bindingRedirect中的app.config中我使用了File Version而不是Assembly Version,这对于这个程序集是不同的 (文件版本= 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>
答案 0 :(得分:2)
我正在使用Visual Studio 2017进行测试,并使用版本Microsoft.Azure.ActiveDirectory.GraphClient
安装包2.1.1
。我还会检查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>
Copy to Output Directory
更改属性Copy if newer
,以确保将配置复制到 bin / debug 文件夹。之后测试成功运行。