0

I have created a (fresh) solution in Visual Studio 2022 17.12.4 with the following projects:

  • ClassLibrary1 (.NET framework 4.8)
    • Has System.Text.Json installed version 9.0.1
    • Has a static method Just.DoIt that calls JsonSerializer.Serialize()
  • ConsoleApp1 (.NET framework 4.8)
    • Has a reference to ClassLibrary1
    • Calls static method Just.DoIt
  • WpfApp1 (.NET framework 4.8)
    • Has a reference to ClassLibrary1
    • Calls static method Just.DoIt
  • UnitTests1 (.NET framework 4.8)
    • Has a reference to ClassLibrary1
    • Calls static method Just.DoIt in a unit test

Here's the Just.DoIt code:

public static class Just
{
    public static void DoIt()
    {
        var person = new Person { FirstName = "Jan", Age = 37, };
        Console.WriteLine(person.ToString());
        string json = JsonSerializer.Serialize(person);
        Console.WriteLine(json);
        var person2 = JsonSerializer.Deserialize<Person>(json);
        Console.WriteLine(person2.ToString());
    }
}

public class Person
{
    [JsonPropertyName("age")]
    public int Age { get; set; }
    [JsonPropertyName("firstname")]
    public string FirstName { get; set; }
    public override string ToString() { return $"{FirstName} ({Age})"; }
}

The console app and wpf app work fine, but the unit test throws an exception when reaching the JsonSerializer.Serialize():

Test method UnitTests1.JustDoItTest.TestMethod1 threw exception: 
System.TypeInitializationException: The type initializer for 'System.Text.Json.JsonSerializer' threw an exception. ---> System.TypeInitializationException: The type initializer for 'PerTypeValues`1' threw an exception. ---> System.IO.FileNotFoundException: 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 system cannot find the file specified.

I have tried the following things that did not solve the issue:

  • Installing System.Runtime.CompilerServices.Unsafe 6.1.0 in every project
  • Creating an App.config with binding redirects in every project.
<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>

and also:

<dependentAssembly>
    <assemblyIdentity name="System.Runtime.CompilerServices.Unsafe" publicKeyToken="b03f5f7f11d50a3a" culture="neutral" />
    <bindingRedirect oldVersion="0.0.0.0-6.1.0.0" newVersion="6.1.0.0" />
</dependentAssembly>
  • Running the tests outside Visual Studio with dotnet test.
  • Adding <CopyLocalLockFileAssemblies>true</CopyLocalLockFileAssemblies> to the unit tests .csproj file.
  • Updated MSTest.TestFramework and MSTest.TestAdapter from 2.2.10 to 3.7.1.
  • Installed Microsoft.TestPlatform in the unit tests project.
  • All the normal steps, like: clean, rebuild, restore and restart Visual Studio.

Do you know how I can solve this issue and not get the exception in the unit test project?

0

1 Answer 1

1

I've been trying to solve this issue for 2 days now and now that I've posted the question I found a solution :$

stackoverflow.com/a/73557076/12924241 Inspired me to change the bindingRedirect to the correct version (6.0.1.0).

This is the correct App.config with binding redirects:

<?xml version="1.0" encoding="utf-8" ?>
<configuration>
  <runtime>
    <assemblyBinding xmlns="urn:schemas-microsoft-com:asm.v1">
      <!--
        This binding redirect is needed to make anything that involves calling System.Text.Json to work.
        For some reason this is only needed in unit test projects.
      -->
      <dependentAssembly>
        <assemblyIdentity name="System.Runtime.CompilerServices.Unsafe" publicKeyToken="b03f5f7f11d50a3a" culture="neutral" />
        <bindingRedirect oldVersion="0.0.0.0-6.0.1.0" newVersion="6.0.1.0" />
      </dependentAssembly>
    </assemblyBinding>
  </runtime>
</configuration>
Sign up to request clarification or add additional context in comments.

5 Comments

Why are you trying to use System.Text.Json Version 9.0 with .NET Framework 4.8? I mean that could work, but going forward, fewer and fewer packages will really be compatible with the old framework.
Because I want to reduce the amount of non-Microsoft nuget packages (like Newtonsoft) in my companies in-house nuget packages. Actually I'm trying to reduce the amount of transitive packages in general. Also, in the example I only use .NET framework projects, but in reality the in-house packages are used by apps varying from .NET framework to .NET 9.
@PMF: Well, the "old" framework (which is btw. preinstalled with every Windows) has a less rapid obsoletion strategy than the .NET [Core] line. When .NET 9 will go out of support in 2026, .NET Framework 4.8 still will be supported. In fact, its end of support date isn't even announced yet. As of today, the the earliest .NET Framework end date is 2027 for .NET Framework 4.6.2. So in fact the System.Text.Json package is also going to support .NET Framework 4.8 much, much longer than .NET 9.
@GyörgyKőszeg I know, but no, there's no guarantee that the new packages will still support the old framework. This one does, but many new packages stop supporting .NET Framework or .NET standard. That .NET Framework is unlikely to go out of support anytime soon does not mean that it still gets new features, so you might be stuck also with old versions of libraries.
@PMF: No one was talking about adding new features to the Framework (though 4.8.1 was quite a surprise). Yes, multiplatform support is often a pain, I know it from experience. I would rather say that it's never guaranteed that a random package will still support the .NET Framework. But we are talking about a System.* package from Microsoft. They won't abandon the .NET Framework until it's supported... by themselves.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.