如何修复错误“无法加载文件或程序集 'System.Text.Json, ...”?

Ale*_*x_P 6 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>
Run Code Online (Sandbox Code Playgroud)

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

当我使用JsonSerializer.Serialize(...). 它在运行时也没有错误或警告地编译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
Run Code Online (Sandbox Code Playgroud)

我在这里缺少什么?

Dud*_*001 6

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

在此处输入图片说明

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

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

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


Sun*_*nny 6

只需安装System.Text.Json的 nuget 包及其所抱怨的版本号即可。