使用 JsonSerializer 时 System.Text.Json 和 System.Runtime.CompilerServices.Unsafe 之间的版本冲突

Ada*_*son 6 .net c# json

我们的 C# 应用程序使用 .Net Framework 4.7.2,我们尝试在 System.Text.Json 中实现 JsonSerializer。我们能够毫无问题地构建解决方案,但是当我们尝试运行该应用程序时,我们会收到以下错误

解决失败:System.Runtime.CompilerServices.Unsafe,Version=4.0.4.1,Culture=neutral,PublicKeyToken=b03f5f7f11d50a3a 运行时捕获异常:Main System.IO.FileLoadException:无法加载文件或程序集“System.Runtime.CompilerServices.Unsafe” 、Version=4.0.4.1、Culture=neutral、PublicKeyToken=b03f5f7f11d50a3a' 或其依赖项之一。找到的程序集的清单定义与程序集引用不匹配。(HRESULT 异常:0x80131040)文件名:'System.Runtime.CompilerServices.Unsafe,Version=4.0.4.1,Culture=neutral,PublicKeyToken=b03f5f7f11d50a3a' at System.Span`1..ctor(T[] array) at System .Text.Json.JsonSerializer.Deserialize[TValue](String json, Type returnType, JsonSerializerOptions options) at Project.Class1.Main(String[] args) in D:\Project\Class1.cs:line 36

查看 NuGet 中安装的 System.Rutime.CompilerServices.Unsafe 版本,它是 5.0.0,在我们的 app.config 文件中,我们有以下内容:

<?xml version="1.0" encoding="utf-8"?>
<configuration>
  <runtime>
    <assemblyBinding xmlns="urn:schemas-microsoft-com:asm.v1">
      <dependentAssembly>
        <assemblyIdentity name="System.Runtime.CompilerServices.Unsafe" publicKeyToken="b03f5f7f11d50a3a" culture="neutral" />
        <bindingRedirect oldVersion="0.0.0.0-5.0.0.0" newVersion="5.0.0.0" />
      </dependentAssembly>
    </assemblyBinding>
  </runtime>
</configuration>
Run Code Online (Sandbox Code Playgroud)

现在,由于 4.0.4.1 在旧版本的范围内,我认为它应该重定向到 5.0.0.0 而不是寻找 4.0.4.1,但我一定是弄错了。我还尝试将旧版本更改为“4.0.4.1”,但这没有帮助。

如果我尝试将 System.Runtime.CompilerServices.Unsafe 的版本更改为包版本 4.5.3(对应于版本 4.0.4.1),则 System.Text.Json 需要从 5.0.2 降级到 2.0.0.11。此版本不包含 JsonSerializer,因此它不适用于我们的应用程序。

有没有一种方法可以让我们成功地从查找旧版本(4.0.4.1)重定向到查找已安装的最新版本(5.0.0.0)?如有任何反馈或建议,我们将不胜感激!