I had a problem when migrating from .NET CORE 5 to .NET CORE 6. I was using Newtonsoft.Json to serialize information in the backend and deserialize the data in the front end. I was using a Tuple object like this to serialize the data:
var content = new Tuple<MainConfiguration, SecondaryConfiguration>(mainConfiguration,secondaryConfiguration);
var serializedContent = JsonConvert.SerializeObject(content);
And was using this code to deserialize the data:
var data = JsonConvert.DeserializeObject<Tuple<MainConfiguration, SecondaryConfiguration>>(serializedContent ?? string.Empty);
With .NET CORE 5 it worked great in development and release.
However after migrating to .NET CORE 6 it only worked in development and not in release. Using Visual Studio everything worked fine, but when publishing the project and executing the published code, I started getting this error message in the Browser console:
ThreadPool Callback threw an unhandled exception of type Newtonsoft.Json.JsonSerializationException
window.Module.s.printErr @ blazor.webassembly.js:1
After reading a lot without results I found the answer here in the comment from v-milindm on May 26. 2022: https://github.com/dotnet/aspnetcore/issues/41748
The problem was the Tuple object, somehow the deserialization was no longer working.
The solution was simple, create an own Tuple object like this:
public class ConfigurationTuple
{
public ConfigurationTuple() { }
public ConfigurationTuple(MainConfiguration item1, SecondaryConfiguration item2)
{
Item1 = item1;
Item2 = item2;
}
public MainConfiguration Item1 { get; set; }
public SecondaryConfiguration Item2 { get; set; }
}
And then replace the .NET Tuple object with my own for serialization:
var content = new ConfigurationTuple(mainConfiguration,secondaryConfiguration);
var serializedContent = JsonConvert.SerializeObject(content);
And in the deserialization:
var data = JsonConvert.DeserializeObject<ConfigurationTuple>(serializedContent ?? string.Empty);
Summary: When migrating from .NET Core 5 to .NET Core 6 and when using Newtonsoft.Json to serialize/deserialize content between the Frontend and Backend, DO NOT USE .NET Tuple object, create your own tuple.
The problem lies in the Front End, not the backend.
This question was created with the sole pourpose of helping others avoid the problem I was having.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With