Bug 39824

Summary: SerializationException thrown when serializing an Exception that uses SerializeObjectState
Product: [Mono] Class Libraries Reporter: David Straw <david.straw>
Component: mscorlibAssignee: Andi McClure <andi.mcclure>
Status: RESOLVED FIXED    
Severity: normal CC: masafa, matt.zinkevicius, mono-bugs+mono, mono-bugs+runtime
Priority: ---    
Version: 4.2.0 (C6)   
Target Milestone: Untriaged   
Hardware: Other   
OS: Linux   
Tags: Is this bug a regression?: ---
Last known good build:
Attachments: Simple reproduction case for the issue

Description David Straw 2016-03-21 22:44:42 UTC
Created attachment 15479 [details]
Simple reproduction case for the issue

In .NET, exception serialization can be implemented either using a deserialization constructor and GetObjectData() or by subscribing to the SerializeObjectState event handler. Doing the latter is apparently not supported in Mono. The problem is that if an Exception class uses this method of serialization, all derived classes must also do so to support serialization. See the "Notes to Implementers" at https://msdn.microsoft.com/en-us/library/system.exception.serializeobjectstate(v=vs.110).aspx

In my particular instance I am deriving from HttpRequestException, which uses the SerializeObjectState event handler to support serialization. I tried doing the same in my derived exception, but it only works in .NET. Under Mono 4.2.2 I get this exception during serialization:

System.Runtime.Serialization.SerializationException: The constructor to deserialize an object of type 'SerializeObjectStateIssueRepro.SerializableException' was not found.
  at System.Runtime.Serialization.ObjectManager.GetConstructor (System.RuntimeType t) <0x7f88d546dfa0 + 0x000aa> in <filename unknown>:0
  at System.Runtime.Serialization.ObjectManager.CompleteISerializableObject (System.Object obj, System.Runtime.Serialization.SerializationInfo info, StreamingContext context) <0x7f88d546dd50 + 0x00120> in <filename unknown>:0
  --- End of inner exception stack trace ---
  at System.Runtime.Serialization.ObjectManager.CompleteISerializableObject (System.Object obj, System.Runtime.Serialization.SerializationInfo info, StreamingContext context) <0x7f88d546dd50 + 0x001c8> in <filename unknown>:0
  at System.Runtime.Serialization.ObjectManager.FixupSpecialObject (System.Runtime.Serialization.ObjectHolder holder) <0x7f88d546be30 + 0x00122> in <filename unknown>:0
  at System.Runtime.Serialization.ObjectManager.DoFixups () <0x7f88d546e050 + 0x000bf> in <filename unknown>:0
  at System.Runtime.Serialization.Formatters.Binary.ObjectReader.Deserialize (System.Runtime.Remoting.Messaging.HeaderHandler handler, System.Runtime.Serialization.Formatters.Binary.__BinaryParser serParser, Boolean fCheck, Boolean isCrossAppDomain, IMethodCallMessage methodCallMessage) <0x7f88d545a2a0 + 0x000bc> in <filename unknown>:0
  at System.Runtime.Serialization.Formatters.Binary.BinaryFormatter.Deserialize (System.IO.Stream serializationStream, System.Runtime.Remoting.Messaging.HeaderHandler handler, Boolean fCheck, Boolean isCrossAppDomain, IMethodCallMessage methodCallMessage) <0x7f88d54546e0 + 0x00165> in <filename unknown>:0
  at System.Runtime.Serialization.Formatters.Binary.BinaryFormatter.Deserialize (System.IO.Stream serializationStream, System.Runtime.Remoting.Messaging.HeaderHandler handler, Boolean fCheck, IMethodCallMessage methodCallMessage) <0x7f88d54546a0 + 0x0002d> in <filename unknown>:0
  at System.Runtime.Serialization.Formatters.Binary.BinaryFormatter.Deserialize (System.IO.Stream serializationStream, System.Runtime.Remoting.Messaging.HeaderHandler handler, Boolean fCheck) <0x7f88d54545c0 + 0x00023> in <filename unknown>:0
  at System.Runtime.Serialization.Formatters.Binary.BinaryFormatter.Deserialize (System.IO.Stream serializationStream, System.Runtime.Remoting.Messaging.HeaderHandler handler) <0x7f88d54545f0 + 0x0001b> in <filename unknown>:0
  at System.Runtime.Serialization.Formatters.Binary.BinaryFormatter.Deserialize (System.IO.Stream serializationStream) <0x7f88d54545a0 + 0x00013> in <filename unknown>:0

I have attached a reproduction case that works in .NET but not in Mono 4.2.2.
Comment 1 Andi McClure 2016-04-14 22:12:52 UTC
Fixed in github master as of PR #2875+#2888 (commit 07c22a0e68b0). With the current schedule the fix will show up in the public release with version 4.6.1.