25
  • 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?

Alex_P
  • 2,580
  • 3
  • 22
  • 37
  • 3
    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` version – Simply Ged Jan 04 '20 at 23:33
  • Which .NET version are you target? – Pavel Anikhouski Jan 05 '20 at 08:24
  • @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. – Alex_P Jan 05 '20 at 22:27
  • 1
    A **work-around** is to use **Newtonsoft.Json**. – Alex_P Jan 30 '20 at 15:46
  • Did you resolve this or find the root cause? – Dude0001 Apr 30 '20 at 01:06
  • @Dude0001, not yet. So far I am using Newtonsoft.Json. – Alex_P Apr 30 '20 at 18:54
  • DLL Hell is still alive and well. :( – Mmm Jul 06 '23 at 18:03

7 Answers7

16

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.

not2qubit
  • 14,531
  • 8
  • 95
  • 135
Dude0001
  • 3,019
  • 2
  • 23
  • 38
5

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

Sunny
  • 2,183
  • 1
  • 17
  • 13
  • 1
    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. – Robson William Feb 23 '23 at 21:17
  • 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. – m_a_s May 18 '23 at 17:24
3

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

FarrukhMalik
  • 129
  • 1
  • 2
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.

sixfeet
  • 934
  • 4
  • 16
  • 33
1

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

Jules
  • 323
  • 2
  • 11
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

Abdullah Tahan
  • 1,963
  • 17
  • 28
0

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

random one
  • 140
  • 1
  • 5
  • 2
    Please explain what changes you made. Red is to be added or removed? Blue-green?? – not2qubit Oct 26 '22 at 12:14
  • 3
    @not2qubit pretty obvious - red is to be removed, green - to be added. It's default Visual Studio "git compare" screenshot. – random one Oct 28 '22 at 17:07
  • While I understood it, it's not obvious to anyone who doesn't use any kind of `compare` software. as @not2qubit try to be as clear as possible in your answers. – RoLYroLLs Mar 28 '23 at 14:25