2

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!

  • I am dealing with this problem at this moment. To make things more interesting, I have a windows application and a VSTO add-in in the same directory and the windows app works fine, loading version 6 of System.Runtime.CompilerServices. Unsafe but the add-in crashes when using JsonSerializer trying to load version 4. – Velja Radenkovic Jul 01 '22 at 13:45
  • For me, the add-in wasn't loading the "addin.dll.config" (with assemblyBinding) because of the adxloader.manifest not being present in the add-in installation directory. Once the "addin.dll.config" was loaded the problem disappeared. My problem along with mentioned manifest is specific for the AddInExpress but I would make sure that you have the correct config file in the program directory. – Velja Radenkovic Jul 01 '22 at 14:17

1 Answers1

0

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.

Rich Shealer
  • 3,362
  • 1
  • 34
  • 59