0

There seems to be automatic (using reflection) and custom serialization in c# (I'm using .NET 5.0 currently)

While Exceptions should be serializable I would assume custom serialization is not strictly needed as automatic serialization can do the job.

Still all online resources I can find, and even Visual Studio and SonarQube issues tell me to implement custom serialization for Exception subclasses when there are any custom fields (in my case an enum and a string, which should both be easy to serialize).

Why is this? Shouldn't it just work fine using automatic serialization?

Koen
  • 174
  • 1
  • 10
  • Serialization for what purpose? – Oliver Weichhold Oct 20 '22 at 10:56
  • @OliverWeichhold Exceptions can be serialised when RPC calls cause an exception at the server, and the exception needs to be serialised back to the client (as an example). – Matthew Watson Oct 20 '22 at 10:58
  • Yeah, what kind of RPC calls? WCF, SOAP, gRPC? – Oliver Weichhold Oct 20 '22 at 11:03
  • I've heard Exceptions in C# should always be serializable in case test runners use AppDomains for example. For example see: https://stackoverflow.com/a/1066714/7857013 – Koen Oct 20 '22 at 11:18
  • I *think* it is intended for .Net remoting. But I do not think .net remoting, or multiple appdomains, are used much in modern applications. By "Automatic serialization" I would assume you mean "binaryFormatter", and this is depricated. Most modern RPC methods use something like json/xml/protobuf etc, and this might need attributes or other accommodations to serialize correctly. – JonasH Oct 20 '22 at 11:27

0 Answers0