c# - 如何修复错误 "Could not load file or assembly ' System.Text.Json, ...”?

标签 c# filenotfoundexception

  • 我在 VS Code 中创建了一个新的 classlib 项目。
  • 我在我的项目中添加了两个包: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
    

    我在这里缺少什么?

    最佳答案

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

    enter image description here

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

    为了解决这个问题,我最终将项目 B 也切换到了目标 dotnetcore3.1,即使它是一个纯库而不是可执行的东西。

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

    关于c# - 如何修复错误 "Could not load file or assembly ' System.Text.Json, ...”?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59595684/

    相关文章:

    java - IO错误: FileNotFound when trying to use jython in java

    java - 线程中的异常 "main"java.io.FileNotFoundException Java

    java - 复制文件导致输出中出现 java.io.FileNotFoundException(拒绝访问)

    c# - 使用azure向IOS和android发送广播通知

    c# - Oracle.DataAccess.dll 未使用 C# 加载

    c# - 将 JSON 作为 .json 文件返回

    c# - 访问 xml 节点值

    c# - 将 MySqlDataReader 记录转换为 ObservableCollection<T>

    java - 如何在 Android 中引用原始文件夹中的文件

    jar 文件中的 Java Filenotfound 异常