How do I fix the error "Could not load file or assembly 'System.Text.Json, ..."?
Asked Answered
D

7

35
  • I created in VS Code a new classlib project.
  • I added two packages to my project: PowerShellStandard.Library + System.Text.Json.

My csproj file contains this block:

  <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.

It does not throw me any error/warning in VS Code when I use JsonSerializer.Serialize(...). It also compiles without errors or warning when runningdotnet 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?

Dun answered 4/1, 2020 at 23:19 Comment(7)
What other dependencies are there? It looks like something else is referencing the System.Text.Json library as the version in the error message does not match the PackageReference versionDisentitle
Which .NET version are you target?Unbonnet
@SimplyGed, I tried to add System.Text.Json --version 4.0.1 but it added version 4.6. Afterwards, I received the same error message. I tried to use NewtonSoft.Json. Even though I added this package to my csproject file VS Code throws the warning that NewtonSoft does not exist in the current context or that the namespace could not be found. @PavelAnikhouski, I want to compile PowerShell functions with C# and want to write it for .NET Core.Dun
A work-around is to use Newtonsoft.Json.Dun
Did you resolve this or find the root cause?Berezniki
@Dude0001, not yet. So far I am using Newtonsoft.Json.Dun
DLL Hell is still alive and well. :(Hutchinson
B
22

I had this issue because I had a dependency on Microsoft.Extensions.Configuration.Json in project B that targeted netstandard. Microsoft.Extensions.Configuration.Json requires System.Text.Json when starting .NETStandard, but not dotnetcore.

enter image description here

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.

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.

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.

Berezniki answered 5/5, 2020 at 19:57 Comment(0)
A
7

Just install the nuget package for the System.Text.Json with the version number that it is complaining about.

Actinal answered 10/12, 2022 at 9:9 Comment(2)
Thanks, this is kind of what I had to do. I had to downgrade System.Text.Json in my class library targeting netstandard2.0 to version 7.0.0. After packing the library and adding it to my .NET Framework 4.7.1 it worked.Embroidery
For what it's worth, I had a WORKING project suddenly, and without changes to the library that handled the JSON files just stop. Would not work until I added System.Text.Json and rebuild.Unstrap
R
5

Simply update the Nuget package or System.Text.Json to 4.7.2 and onwards.

Rios answered 28/2, 2021 at 17:40 Comment(0)
G
4

I got the same exact error stating that it could not find system.text.json assembly version 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. So I had updated a dependency that used system.text.json to a .NET 7 version by accident.

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

Gryphon answered 23/11, 2022 at 14:53 Comment(1)
This was my exact problem as well. Thanks.Anecdote
T
1

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". 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.

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.

Uninstalled the IdentityModel and reinstalled it, and the problem was fixed.

Ti answered 22/3, 2022 at 6:23 Comment(0)
R
1

These changes in .csproj file helped me (everything else didn't)

Respectful answered 5/10, 2022 at 7:54 Comment(3)
Please explain what changes you made. Red is to be added or removed? Blue-green??Demagnetize
@Demagnetize pretty obvious - red is to be removed, green - to be added. It's default Visual Studio "git compare" screenshot.Respectful
While I understood it, it's not obvious to anyone who doesn't use any kind of compare software. as @Demagnetize try to be as clear as possible in your answers.Enterogastrone
I
1

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
and internally it uses one of the packages that depends on 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 enter image description here

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 enter image description here

so the solution was to downgrade the plugin to the same package that was used by the main process

Infuriate answered 26/1, 2023 at 23:6 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.