System.Text.Json is throwing TypeInitializationError

Viewed 839

I am trying to make a Revit 2021 add-in in .Net Framework 4.8 and I get this exception when trying to deserialize a JSON.

The code is rather simple. First, I read the text from a .json file, then I call the method Deserialize() and on that same line, the exception occurs.

string json = File.ReadAllText(@"D:\CDMModel.json");

JsonObject model = JsonSerializer.Deserialize<JsonObject>(json);

An exception of type 'System.TypeInitializationException' occurred in Revit2021Json.dll but was not handled in user code Additional information: The type initializer for 'System.Text.Json.JsonSerializer' threw an exception.

The inner exception is:

System.TypeInitializationException: The type initializer for 'System.MemoryExtensions' threw an exception. ---> System.IO.FileNotFoundException: Could not load file or assembly 'System.Runtime.CompilerServices.Unsafe, Version=4.0.4.1

What could be the cause of this?

P.S. Because of privacy matters, I am not able to share the json and the object, therefore I am not expecting a fully working answer. However, it would be great if we could discuss this at a purely theoretical level, if possible.

0 Answers
Related