dotnet / WatsonTcp

WatsonTcp is the easiest way to build TCP-based clients and servers in C#.

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

System.NotSupportedException when SerializeJson is called with an exception as parameter

fisherman6v6 opened this issue · comments

Hi!
I've found that when a Connect() fails instead of throwing the correct SocketException, a System.NotSupportedException is thrown by System.Text.Json with the following message:

'Serialization and deserialization of 'System.Reflection.MethodBase' instances are not supported. Path: $.TargetSite.'

Apparently System.Text.Json does not handle correctly exception serialization as stated here for example https://stackoverflow.com/questions/71132371/why-does-system-text-json-throw-a-notsupportedexception-for-system-intptr-wh

Could this be corrected by avoiding the full exception serialization?

Hi, this is a biproduct of moving away from Newtonsoft. Newtonsoft is capable of serializing exceptions, and System.Text.Json has some issues with it. You're welcome to implement your own serializer and attach it to SerializationHelper if you wish to either revert to Newtonsoft, or, if you wish to implement your own methods and converters to better handle them.

Hi @fisherman6v6 alternatively if you have a converter for Exception that you'd like to include, I'd be happy to incorporate either directly or via a PR! I'm searching for one, because I think this is a common use case.

Wouldn't be enough to pass back just the exception object inside the Exception property and leave the management to the user?
In other words, is it necessary to serialize the whole exception?

Hi @fisherman6v6 I misunderstood what you were saying. I see the issue now, there are several places where ExceptionEventArgs are passed which have a Json property, which doesn't work correctly without a custom converter. Addressing this now!

Hi @fisherman6v6 NuGet v5.0.10 removes this property. Please let me know if there are other areas you see that I've missed where exception objects are serialized, or please close if this fixes the issue for you.

NuGet: https://www.nuget.org/packages/WatsonTcp/5.0.10
Commit: 1aed91d