Our C# application is using .Net Framework 4.7.2 and we're trying to implement JsonSerializer in System.Text.Json. We are able to build the solution without any issues but when we try to run the application we get the following error
Resolve failed: System.Runtime.CompilerServices.Unsafe, Version=4.0.4.1, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a Caught exception while running: Main System.IO.FileLoadException: 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. (Exception from HRESULT: 0x80131040) File name: 'System.Runtime.CompilerServices.Unsafe, Version=4.0.4.1, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a' at System.Span`1..ctor(T[] array) at System.Text.Json.JsonSerializer.Deserialize[TValue](String json, Type returnType, JsonSerializerOptions options) at Project.Class1.Main(String[] args) in D:\Project\Class1.cs:line 36
Looking at the version of System.Rutime.CompilerServices.Unsafe that is installed in NuGet, it's 5.0.0 and inside our app.config file, we have the following:
<?xml version="1.0" encoding="utf-8"?>
<configuration>
<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-5.0.0.0" newVersion="5.0.0.0" />
</dependentAssembly>
</assemblyBinding>
</runtime>
</configuration>
Now, since 4.0.4.1 is within the range of the oldVersion, I would assume that it should be redirecting to 5.0.0.0 instead of looking for 4.0.4.1, but I must be mistaken. I also tried to change the oldVersion to "4.0.4.1" but that did not help.
If I try to change the version of System.Runtime.CompilerServices.Unsafe to the package version 4.5.3, which corresponds to version 4.0.4.1, then System.Text.Json needs to downgraded from 5.0.2 to 2.0.0.11. This version does not contain JsonSerializer so it won't work with our application.
Is there a way that we could successfully redirect from looking for the old version (4.0.4.1) to looking for the latest version that is installed (5.0.0.0)? Any feedback or suggestions is appreciated!