Reputation: 51
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!
Upvotes: 5
Views: 3587
Reputation: 1449
I've a similar issue with a .Net Framework project and I see that when you add a NuGet package that uses System.Text.Json
, Visual Studio adds the below assembly binding redirect to the app.config.
<?xml version="1.0" encoding="utf-8"?>
<configuration>
<startup>
<supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.7.2" />
</startup>
<runtime>
<assemblyBinding xmlns="urn:schemas-microsoft-com:asm.v1">
<dependentAssembly>
<assemblyIdentity name="System.Runtime.CompilerServices.Unsafe" publicKeyToken="b03f5f7f11d50a3a" culture="neutral" />
<bindingRedirect oldVersion="0.0.0.0-6.0.0.0" newVersion="6.0.0.0" />
</dependentAssembly>
<dependentAssembly>
<assemblyIdentity name="System.Memory" publicKeyToken="cc7b13ffcd2ddd51" culture="neutral" />
<bindingRedirect oldVersion="0.0.0.0-4.0.1.2" newVersion="4.0.1.2" />
</dependentAssembly>
</assemblyBinding>
</runtime>
</configuration>
I think this is the way Microsoft chooses to solve this issue. Tested with VS2019 and VS2022.
Upvotes: 2
Reputation: 3524
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.
Upvotes: 2