Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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

like image 449
Alex_P Avatar asked Jan 04 '20 23:01

Alex_P


1 Answers

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.

like image 194
Dude0001 Avatar answered Sep 29 '22 10:09

Dude0001