dotnet / aspnetcore

ASP.NET Core is a cross-platform .NET framework for building modern cloud-based web applications on Windows, Mac, or Linux.
https://asp.net
MIT License
35.46k stars 10.03k forks source link

Type of strings arriving in Hub have changed in .Net Core 3.0 #16963

Closed samcov closed 5 years ago

samcov commented 5 years ago

Basic Issue: In .net Core 2.2, passing an array into a hub method maintained the expected type, in 3.0, the type for strings is not preserver when getting the type via GetType, but shows up properly in debugging.

I expect a string, but I get {Name = "JsonElement" FullName = "System.Text.Json.JsonElement"}

View of the Hub Method: public async Task ExecRemoteFunctionAsync(string functionname, object[] parameters, string fullclass, string token)

It's the parameters we want to inspect because we want to do type conversion to match the signature of the function being called.

However, when we do the following, if (value.GetType() == targetType), where target type is string, we see value as string in debugging, but the result of applying GetType is as shown above.

This does not happen in .net core 2.2, GetType returns a string.

I actually believe the 3.0 version is correct, but this is a breaking change if we have to handle Json elements as well as normal situations.

BrennanConroy commented 5 years ago

In 3.0 the default hub protocol was changed from using Newtonsoft.Json to System.Text.Json. There are many differences between the two libraries. If you would like to switch back to using Newtonsoft.Json then you can do so by following the docs: https://docs.microsoft.com/aspnet/core/migration/22-to-30?view=aspnetcore-3.0&tabs=visual-studio#switch-to-newtonsoftjson

samcov commented 5 years ago

@BrennanConroy That was the problem, following the directions in the docs resolved my issue. I'll want to use the default in the future, but it seems to have radically different behavior.

Either way, many thanks to you for recognizing the problem and supplying a solution!