简体   繁体   English

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

[英]How do I fix the error "Could not load file or assembly 'System.Text.Json, ..."?

  • I created in VS Code a new classlib project.我在 VS Code 中创建了一个新的类库项目。
  • I added two packages to my project: PowerShellStandard.Library + System.Text.Json .我在我的项目中添加了两个包: PowerShellStandard.Library + System.Text.Json

My csproj file contains this block:我的csproj文件包含这个块:

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

My .cs file uses System.Text.Json and System.Management.Automation .我的.cs文件使用System.Text.JsonSystem.Management.Automation

It does not throw me any error/warning in VS Code when I use JsonSerializer.Serialize(...) .当我使用JsonSerializer.Serialize(...)时,它不会在 VS Code 中抛出任何错误/警告。 It also compiles without errors or warning when running do.net build .它在运行do.net build时也编译无错误或警告。 I can import it but, finally, when I run the code I receive the following error:我可以导入它,但最后,当我运行代码时,我收到以下错误:

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

What am I missing here?我在这里错过了什么?

I had this issue because I had a dependency on Microsoft.Extensions.Configuration.Json in project B that targeted netstandard.我遇到这个问题是因为我依赖于针对 netstandard 的项目 B 中的 Microsoft.Extensions.Configuration.Json。 Microsoft.Extensions.Configuration.Json requires System.Text.Json when starting .NETStandard, but not dotnetcore.启动 .NETStandard 时,Microsoft.Extensions.Configuration.Json 需要 System.Text.Json,但不需要 dotnetcore。

在此处输入图片说明

My problem came when in project A that targeted dotnetcore3.1 referenced project B. At runtime, AWS Lambda was still expecting System.Text.Json to be there.我的问题出现在针对 dotnetcore3.1 引用项目 B 的项目 A 中。在运行时,AWS Lambda 仍然期望 System.Text.Json 存在。

To resolve the issue, I ended up switching project B to target dotnetcore3.1 as well, even though it is a pure library and not something executable.为了解决这个问题,我最终将项目 B 也切换到了目标 dotnetcore3.1,即使它是一个纯库而不是可执行的东西。

Not sure this answers your question directly as I don't fully understand your situation, but a possible solution for this scenario.不确定这是否能直接回答您的问题,因为我不完全了解您的情况,但是这种情况的可能解决方案。 It was a little difficult finding many other resources on this issue but I probably just don't know what to look for.在这个问题上找到许多其他资源有点困难,但我可能只是不知道该寻找什么。

I had this issue recently with the following message: "Could not load file or assembly 'System.Text.Json, Version=6.0.0.0, Culture=neutral, PublicKeyToken=cc7b13ffcd2ddd51".我最近遇到了这个问题,消息如下:“无法加载文件或程序集‘System.Text.Json,Version=6.0.0.0,Culture=neutral,PublicKeyToken=cc7b13ffcd2ddd51”。 I had updated the nuget packages of the System.Text.Json (6.0.2) and IdentityModel (6.0.0) as part of a nuget update operation across the projects.作为跨项目的 nuget 更新操作的一部分,我已经更新了 System.Text.Json (6.0.2) 和 IdentityModel (6.0.0) 的 nuget 包。

The culprit was the this latest version of System.Text.Json (6.0.2) which was not compatible as a dependency for IdentityModel package latest version (6.0.0), which requires System.Text.Json, Version=6.0.0.0.罪魁祸首是最新版本的 System.Text.Json (6.0.2),它作为 IdentityModel package 最新版本 (6.0.0) 的依赖项不兼容,后者需要 System.Text.Json,Version=6.0.0.0。

Uninstalled the IdentityModel and reinstalled it, and the problem was fixed.卸载了 IdentityModel 并重新安装,问题就解决了。

I got the same exact error stating that it could not find system.text.json assembly version 7.0.0.我得到了同样的错误,指出它找不到 system.text.json 程序集版本 7.0.0。

After wasting half a day I realized that I had recently update a bunge of dependencies, and that.Net 7 was also released only a 2 weeks ago.浪费了半天之后,我意识到我最近更新了一堆依赖项,而 .Net 7 也是在 2 周前才发布的。 So I had updated a dependency that used system.text.json to a .NET 7 version by accident.所以我不小心将使用 system.text.json 的依赖项更新为 .NET 7 版本。

Solution for me was updating Visual Studio to the latest version that came with .NET 7 SDK and updating the project target framework to .NET 7我的解决方案是将 Visual Studio 更新为 .NET 7 SDK 附带的最新版本,并将项目目标框架更新为 .NET 7

Just install the nuget package for the System.Text.Json with the version number that it is complaining about.只需为System.Text.Json安装 nuget package 和它抱怨的版本号。

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

These changes in.csproj file helped me (everything else didn't) .csproj 文件中的这些更改帮助了我(其他一切都没有)

I spent two days on this and it seems that you are facing a dll hell basically in my case I used a plugin which attached to the main application我在这上面花了两天时间,看来你正面临dll 地狱基本上在我的情况下我使用了一个附加到主应用程序的插件
and internally it uses one of the packages that depends on System.Text.Json在内部它使用依赖于 System.Text.Json 的包之一

the exception was例外是

Could not load file or assembly 'System.Text.Json, Version=6.0.0.0, Culture=neutral, PublicKeyToken=cc7b13ffcd2ddd51' or one of its dependencies. The system cannot find the file specified.

which doesn't really help but seeing the fusionLog indicates that the package IdentityModel was depending on System.Text.Json so when checked the dlls was correctly there这并没有真正帮助,但看到fusionLog表明 package IdentityModel 依赖于 System.Text.Json 所以当检查时 dll 正确存在在此处输入图像描述

So i have to use Process Explorer which helped me to understand which and assembly was loaded and here's the surprise we have dll hell which means my application loaded different assembly of same dll but with older version所以我必须使用Process Explorer ,它帮助我了解加载了哪个和程序集,这是我们有 dll 地狱的惊喜,这意味着我的应用程序加载了相同的不同程序集 dll 但使用旧版本在此处输入图像描述

so the solution was to downgrade the plugin to the same package that was used by the main process所以解决方案是将插件降级为与主进程使用的相同的 package

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Azure Function 使用 Graph 我得到“无法加载文件或程序集‘System.Text.Json’” - Azure Function Using Graph I get "Could not load file or assembly 'System.Text.Json" 在 Godot C# android 构建中使用 System.Text.Json 时无法加载文件或程序集“System.Numerics.Vectors” - Could not load file or assembly 'System.Numerics.Vectors' when using System.Text.Json in Godot C# android build 如何修复“无法加载文件或程序集System.Core”错误? - How to fix “Could not load file or assembly System.Core” error? 如何避免使用 System.Text.Json 编写`$type`? - How do I avoid writing `$type` with System.Text.Json? 如何通过 System.Text.Json 从文件加载值并将它们存储为只读? - How to load values via System.Text.Json from a file and store them readonly? 如何反序列化嵌套的 JSON object,这是 System.Text.Json 中的一个字符串? - How do I deserialize a nested JSON object which is a string in System.Text.Json? 使用 System.Text.Json,如何从扩展数据对象中获取“对象”? - Using System.Text.Json, how do I get a "object" out of a extension data object? ASP Core Minimal API - 如何使用 System.Text.Json 发送不带尾随零的小数? - ASP Core Minimal API - How do I send decimals without trailing zeros with System.Text.Json? System.Text.Json:如何将 object 序列化为带有键的嵌套子对象? - System.Text.Json: How do I serialize an object to be a nested subobject with a key? System.Text.Json:如何为枚举值指定自定义名称? - System.Text.Json: How do I specify a custom name for an enum value?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM