【问题标题】:How do I fix the error "Could not load file or assembly 'System.Text.Json, ..."?如何修复错误“无法加载文件或程序集'System.Text.Json,...”?
【发布时间】:2020-01-04 23:19:52
【问题描述】:
  • 我在 VS Code 中创建了一个新的类库项目。
  • 我在我的项目中添加了两个包:PowerShellStandard.Library + System.Text.Json

我的csproj 文件包含此块:

  <ItemGroup>
    <PackageReference Include="PowerShellStandard.Library" Version="5.1.1" />
    <PackageReference Include="System.Text.Json" Version="4.7.0" />
  </ItemGroup>

我的.cs 文件使用System.Text.JsonSystem.Management.Automation

当我使用JsonSerializer.Serialize(...) 时,它不会在 VS Code 中向我抛出任何错误/警告。在运行dotnet build 时,它也可以无错误或警告地编译。我可以导入它,但最后,当我运行代码时,我收到以下错误:

Get-JsonString : Could not load file or assembly 'System.Text.Json, Version=4.0.1.0, Culture=neutral, PublicKeyToken=cc7b13ffcd2ddd51'. The system cannot find the file specified.
At line:1 char:1
+ Get-JsonString -input s
+ ~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo          : NotSpecified: (:) [Get-JsonString], FileNotFoundException
+ FullyQualifiedErrorId : System.IO.FileNotFoundException,UrlCSharpPowerShell.CreateJson

我在这里错过了什么?

【问题讨论】:

  • 还有哪些依赖?看起来其他东西正在引用System.Text.Json 库,因为错误消息中的版本与PackageReference 版本不匹配
  • 您的目标是哪个 .NET 版本?
  • @SimplyGed,我尝试添加 System.Text.Json --version 4.0.1,但它添加了 4.6 版。之后,我收到了同样的错误信息。我尝试使用NewtonSoft.Json。即使我将此包添加到我的 csproject 文件中,VS Code 也会引发警告,即当前上下文中不存在 NewtonSoft 或找不到命名空间。 @PavelAnikhouski,我想用 C# 编译 PowerShell 函数并想为 .NET Core 编写它。
  • 一个解决方法是使用Newtonsoft.Json
  • 您解决了这个问题还是找到了根本原因?

标签: c# filenotfoundexception


【解决方案1】:

我遇到了这个问题,因为我在项目 B 中依赖于针对 netstandard 的 Microsoft.Extensions.Configuration.Json。 Microsoft.Extensions.Configuration.Json 在启动 .NETStandard 时需要 System.Text.Json,但不是 dotnetcore。

我的问题出现在以 dotnetcore3.1 为目标的项目 A 中引用了项目 B。在运行时,AWS Lambda 仍然期望 System.Text.Json 存在。

为了解决这个问题,我最终将项目 B 也切换到了 dotnetcore3.1,尽管它是一个纯库而不是可执行文件。

不确定这是否直接回答了您的问题,因为我不完全了解您的情况,但可能是这种情况的解决方案。在这个问题上找到许多其他资源有点困难,但我可能只是不知道该寻找什么。

【讨论】:

    【解决方案2】:

    只需将 Nuget 包或 System.Text.Json 更新到 4.7.2 及更高版本。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2010-10-07
      • 2023-03-05
      • 2018-04-10
      • 1970-01-01
      • 1970-01-01
      • 2022-11-23
      相关资源
      最近更新 更多