System.Text.Json 6.0.2 Serialize error - trying to load System.Runtime.CompilerServices.Unsafe, Version=4.0.4.1

Viewed 607

We are using C# .Net Framework 4.7.2, JsonSerializer in System.Text.Json Version 6.0.2. Solution builds fine. We get a runtime error when Serializing: Could not load file or assembly 'System.Runtime.CompilerServices.Unsafe, Version=4.0.4.1, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a' or one of its dependencies. The located assembly's manifest definition does not match the assembly reference.

I confirmed that System.Runtime.CompilerServices.Unsafe Assembly Version 6.0.0 isinstalled as part of the NuGet package. And that App.config contains assembly redirect lines pointing to installed Version:

name="System.Runtime.CompilerServices.Unsafe" publicKeyToken="b03f5f7f11d50a3a" culture="neutral"

bindingRedirect oldVersion="0.0.0.0-6.0.0.0" newVersion="6.0.0.0"

Appreciate any help on how to resolve this - why is System.Runtime.CompilerServices.Unsafe, Version=4.0.4.1 needed at Runtime. Thanks!

1 Answers

I had a similar error with Visual Studio 2022 Version 17.1.1 with a new .Net 4.8 WinForms application.

After some hints elsewhere, I opened the NuGet package manger for the solution.

Tools/NuGet Package Manager/Manage NuGet Packages for the Solution...

From there I searched for and added this package to the project that used JsonSerializer.Serialize().

System.Runtime.CompilerServices.Unsafe (V6.0.0)

To be clear this was not a listed installed packaged in the NuGet manager previously.

The runtime exception error changed to:

System.IO.FileNotFoundException HResult=0x80070002 Message=Could not load file or assembly 'System.Threading.Tasks.Extensions, Version=4.2.0.1, Culture=neutral, PublicKeyToken=cc7b13ffcd2ddd51' or one of its dependencies. The system cannot find the file specified. Source=System.Text.Json

So I then searched for and added to the same project: System.Threading.Tasks.Extensions (V4.5.4)

Again this was not a listed installed package in the NuGet manager before this install.

So far everything continues to work. I did not have to add these packages to the project that defined the class being serialized. Just the one that did the serialization.

Related