c# - FileLoadException : Moq and Moq. 资源

标签 c# visual-studio tfs moq autofixture

我在使用 Moq 和 AutoFixture.AutoMoq 的一个特定项目的构建服务器上运行单元测试时遇到问题。 这是错误:

System.IO.FileLoadException : Could not load file or assembly 'Moq, Version=4.1.1308.2120, 
Culture=neutral, PublicKeyToken=69f491c39445e920' or one of its dependencies. The located 
assembly's manifest definition does not match the assembly reference. 
(Exception from HRESULT: 0x80131040) at Ploeh.AutoFixture.AutoMoq.MockPostprocessor.Create(Object 
request, ISpecimenContext context)

该项目基于 .NET 4.5.1 构建,并使用以下 nuget 包(整个解决方案中也使用了这些版本):

<package id="AutoFixture" version="3.40.1" targetFramework="net451" />
<package id="AutoFixture.AutoMoq" version="3.40.1" targetFramework="net451" />
<package id="Moq" version="4.2.1510.2205" targetFramework="net451" />

这是失败的测试之一(使用 xunit 2.0):

[Fact]
public void SutIsIMessageConverterService()
{
    var fixture = new Fixture().Customize(new AutoMoqCustomization());
    var sut = fixture.Create<XmlValidationConverterService>();
    Assert.IsAssignableFrom<IMessageConverterService>(sut);
}

该项目有程序集绑定(bind)重定向(但无济于事):

<assemblyBinding xmlns="urn:schemas-microsoft-com:asm.v1">
  <dependentAssembly>
    <assemblyIdentity name="Moq" publicKeyToken="69f491c39445e920" culture="neutral" />
    <bindingRedirect oldVersion="0.0.0.0-4.2.1510.2205" newVersion="4.2.1510.2205" />
  </dependentAssembly>
</assemblyBinding>

在构建服务器上使用FUSLOGVW.exe,我发现了一些更多信息(起初我假设 Moq 没有任何依赖项):

LOG: DisplayName = Moq.resources, Version=4.2.1510.2205, Culture=en-US, PublicKeyToken=69f491c39445e920 (Fully-specified)
LOG: Appbase = file:///D:/ProjectName.Tests
LOG: Initial PrivatePath = NULL
LOG: Dynamic Base = NULL
LOG: Cache Base = C:\Users\BuildServerUserName\AppData\Local\Temp\ddf02945-045c-408c-a648-ec5325032f0a
LOG: AppName = ddf02945-045c-408c-a648-ec5325032f0a
Calling assembly : Moq, Version=4.2.1510.2205, Culture=neutral, PublicKeyToken=69f491c39445e920.
LOG: This bind starts in default load context.
LOG: Using application configuration file: D:\ProjectName.Tests.dll.config
LOG: Using host configuration file: 
LOG: Using machine configuration file from C:\Windows\Microsoft.NET\Framework\v4.0.30319\config\machine.config.
LOG: Post-policy reference: Moq.resources, Version=4.2.1510.2205, Culture=en-US, PublicKeyToken=69f491c39445e920
LOG: GAC Lookup was unsuccessful.
LOG: Attempting download of new URL file:///D:/ProjectName.Tests/en-US/Moq.resources.DLL.
LOG: Attempting download of new URL file:///D:/ProjectName.Tests/en-US/Moq.resources/Moq.resources.DLL.
LOG: Attempting download of new URL file:///D:/ProjectName.Tests/en-US/Moq.resources.EXE.
LOG: Attempting download of new URL file:///D:/ProjectName.Tests/en-US/Moq.resources/Moq.resources.EXE.
LOG: All probing URLs attempted and failed.

因此 Moq 依赖于 Moq.resources 并且无法找到它。

有谁知道它为什么要寻找这个(它是什么?)以及我如何解决这个问题?

最佳答案

尝试 this 中的解决方案博客:

  • 从任何 .config 文件中删除该元素及其子元素。
  • 在 Visual Studio 中打开包管理器控制台。这可以通过 View | 来完成。其他窗口|包管理器控制台菜单。
  • 输入这个神奇的命令可以解决所有问题:Get-Project -All |添加-BindingRedirect

关于c# - FileLoadException : Moq and Moq. 资源,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37585696/

相关文章:

c# - wsdl.exe 能否生成与 Visual Studio 在添加 Web 引用时生成的文件相同的文件?

具有更多受让人的 TFS 任务

tfs - 如何仅构建特定项目及其与 DevOps Pipelines 的依赖关系

c# - 使用带有表名变量的 c#.net 创建数据库表

c# - 十进制 ToString F02 变成逗号

c# - Nhibernate 查询超时而 sql 查询没有

c# - 无法引用该项目。引用的项目针对不同的框架系列

ios - Cordova iOS 构建在 Release模式下失败

tfs - NuGet 包仅在 TFS 管理的提要中显示为预发布

c# - .NET/C# 中的 TreeView 双击行为