title: System.Text.Json Deserialize Error System.MemoryExtensions Cannot Load System.Runtime.CompilerServices.Unsafe
body: <p>Similar to <a href="https://stackoverflow.com/questions/68398579/conflict-of-version-between-system-text-json-and-system-runtime-compilerservices">another question</a>, but with different symptoms and I didn't want to ask a question within a question.</p>
  <p>Using VS2017 with all projects targeting .NET Framework 4.8.  Tried oldest (4.6.0) and newest (5.0.2) versions of System.Text.Json.  Also tried targeting all projects at .NET Framework 4.6.1.  I built three projects:</p>
  <ul>
  <li><p>MyClassLib - class library containing classes and code to built
  object instances from JSON returned from T-SQL queries</p>
  </li>
  <li><p>MyServiceLib - service library referencing and exposing certain
  functions within MyClassLib.  Launched in Debug with typical WcfSvcHost.exe/WcfTestClient.exe treatment as MyService</p>
  </li>
  <li><p>MyTestClient - WinForms client referencing both MyClassLib and
  MyService (through Debug hosting of MyServiceLib) to test functionality.  Reused objects in MyClassLib in referencing MyService</p>
  </li>
  </ul>
  <p>MyClassLib function performs perfectly when called from WinForms app, but gives the error below when called from service library.  Specific line of code where it fails:</p>
  <pre><code>Empl[] empls = JsonSerializer.Deserialize&lt;Empl[]&gt;(jsonString);
  </code></pre>
  <p>Inside MyClassLib, everything is <strong>exactly</strong> the same between the two call paths.  What can be the difference?!?!</p>
  <pre><code>System.TypeInitializationException: The type initializer for 'System.Text.Json.JsonSerializer' threw an exception. ---&gt; System.TypeInitializationException:   The type initializer for 'System.MemoryExtensions' threw an exception. ---&gt; 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)    at System.MemoryExtensions.MeasureStringAdjustment()    at System.MemoryExtensions..cctor()    --- End of inner exception stack trace ---    at System.MemoryExtensions.AsSpan(String text)    at System.Text.Json.JsonEncodedText.Encode(String value, JavaScriptEncoder encoder)    at System.Text.Json.JsonSerializer..cctor()    --- End of inner exception stack trace ---    at System.Text.Json.JsonSerializer.Deserialize[TValue](String json, JsonSerializerOptions options)    at GlobAppSecLib.GlobAppSec.RetEmplSecurity(String EmplNetworkID, String AppName) in C:\\[blah]\\MyClassLibModule.cs:line 155
  
  System.TypeInitializationException: The type initializer for 'System.Text.Json.JsonSerializer' threw an exception. ---&gt; System.TypeInitializationException:   The type initializer for 'System.MemoryExtensions' threw an exception. ---&gt; 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)    at System.MemoryExtensions.MeasureStringAdjustment()    at System.MemoryExtensions..cctor()    --- End of inner exception stack trace ---    at System.MemoryExtensions.AsSpan(String text)    at System.Text.Json.JsonEncodedText.Encode(String value, JavaScriptEncoder encoder)    at System.Text.Json.JsonSerializer..cctor()    --- End of inner exception stack trace ---    at System.Text.Json.JsonSerializer.Deserialize[TValue](String json, JsonSerializerOptions options)    at GlobAppSecLib.GlobAppSec.RetEmplSecurity(String EmplNetworkID, String AppName) in C:\\[blah]\\MyClassLibModule.cs:line 155
  </code></pre>
  <p>I notice the DLLs dragged along by System.Json.Text are a mix of 'Product name' Microsoft .NET Core and Microsoft .NET Framework:</p>
  <ul>
  <li>System.Text.Json.dll = Microsoft .NET Core</li>
  <li>System.Memory.dll = Microsoft .NET Framework</li>
  <li>System.Runtime.CompilerServices.Unsafe.dll = Microsoft .NET Framework</li>
  </ul>
  <p>Just noticed that the WinForms client runs as 32-bit while WcfSvcHost.exe runs as 64-bit.</p>
  <p>Building a WinForms service host instead of using WcfSvcHost.exe was one approach to 'fixing' the problem.  Part 1 of <a href="https://stackoverflow.com/questions/62764744/could-not-load-file-or-assembly-system-runtime-compilerservices-unsafe/62770487#62770487">this answer</a> suggested by @sean-skelly included a link to <a href="https://www.nuget.org/packages/System.Runtime.CompilerServices.Unsafe/4.5.3" rel="nofollow noreferrer">the System.Runtime.CompilerServices.Unsafe NuGet package</a>.  Playing with that has made me conclude you can't get the 4.0.4.1 version of the dll to coexist with 5.0.2 version of System.Text.Json.  Smells like a Microsoft screw up.  Moved everything to VS 2019 and used latest versions, but to no avail.  Part 2 of answer suggested by @sean-skelly led to <a href="https://docs.microsoft.com/en-us/dotnet/framework/configure-apps/file-schema/runtime/bindingredirect-element" rel="nofollow noreferrer">an article about bindingRedirect</a> that seems somewhat helpful.</p>