Class 库中的 XUnit 和 F# 核心 4.3.1 加载错误

XUnit and F# core 4.3.1 loading errors when in Class Library

我对(可能)XUnit 和许多 Nuget 包有这个非常困惑的问题。我的解决方案包括一个与此问题无关的 C# 项目和一个 F# 测试项目。

测试项目是一个非常简单的 Class 库项目,包含一个包含所有测试的 .fs 文件和一些 NuGet 包,其中三个是 (IMO) 重要的:fsUnitxUnit 版本1.4.1.0,2.1.0 版为 XUnit,4.0.0.1 版为 FSharp.Core

问题是当以下两个条件都为真时,测试总是在 Could not load file or assembly FSharp.Core, Version=4.3.1.0 上失败:引用的 NuGet FSharp.Core 是版本 4.0.0.[10]并且项目的类型是 Class library.

当 NuGet FSharp.Core 手动降级到版本 3.1.2.5 或项目的类型更改为 Console Aplication 时,不会抛出异常并且所有测试都按预期通过。

好像这还不够奇怪,它只在 Windows 上这样做。在 Linux(或至少是 travis-cl(如图 here))上,所有配置都运行良好。

已链接 here 正在测试回购。最后三个提交显示哪些配置有效,哪些无效。最后的 "does also work" 配置没有合适的 travis 配置文件,但在 Windows 和 Travis 上都有效(测试通过)。

免责声明:我对 NuGet 包完全陌生,所以我可能会遗漏一些非常基本的东西。

问题是 FsUnit.xUnit 引用 FSharp.Core 4.3.1.0 而您的项目使用的是不同的版本。

您应该可以通过使用 binding redirect.

来解决这个问题
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
    ...
    <runtime>
      <assemblyBinding xmlns="urn:schemas-microsoft-com:asm.v1">
        <dependentAssembly>
          <assemblyIdentity name="FSharp.Core" publicKeyToken="b03f5f7f11d50a3a" culture="neutral" />
          <bindingRedirect oldVersion="0.0.0.0-4.3.0.0" newVersion="4.3.1.0" />
        </dependentAssembly>
      </assemblyBinding>
    </runtime>
</configuration>

我相信您应该能够将其放入您的测试项目的 app.config 并且 xunit 应该可以找到并使用生成的 .dll.config 文件。我会先试试看。否则,您可以将绑定重定向放入 xunit.console.exe.config,以便在您 运行 xunit.console.exe.

时使用它