如何解决 "无法加载文件或程序集'System.Text.Json,...'"的错误?

问题描述 投票:2回答:1
  • 我在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.

在VS Code中,当我使用 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

我缺少什么?

c# filenotfoundexception
1个回答
3
投票

我之所以出现这个问题,是因为我在项目B中对Microsoft.Extensions.Configuration.Json有一个针对netstandard的依赖。Microsoft.Extensions.Configuration.Json在启动.NETStandard时需要System.Text.Json,但不需要dotnetcore。

enter image description here

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

为了解决这个问题,我最后把项目B也换成了以dotnetcore3.1为目标的项目,尽管它是一个纯库而不是可执行的东西。

不知道这能不能直接回答你的问题,因为我并不完全了解你的情况,但对这种情况提供了一个可能的解决方案。在这个问题上找到很多其他资源有点困难,但我可能就是不知道该找什么。

© www.soinside.com 2019 - 2024. All rights reserved.