Bug 695203 - KnownType for Derived Class not Resolved
Summary: KnownType for Derived Class not Resolved
Status: NEW
Alias: None
Product: Mono: Class Libraries
Classification: Mono
Component: WCF (show other bugs)
Version: 2.10.x
Hardware: x86 openSUSE 11.3
: P5 - None : Normal
Target Milestone: ---
Assignee: Atsushi Enomoto
QA Contact: Mono Bugs
URL:
Whiteboard:
Keywords:
Depends on:
Blocks:
 
Reported: 2011-05-20 17:24 UTC by Cristiano Simionato
Modified: 2011-05-20 17:24 UTC (History)
0 users

See Also:
Found By: ---
Services Priority:
Business Priority:
Blocker: ---
Marketing QA Status: ---
IT Deployment: ---


Attachments

Note You need to log in before you can comment on or make changes to this bug.
Description Cristiano Simionato 2011-05-20 17:24:39 UTC
User-Agent:       Mozilla/5.0 (Windows NT 6.1; WOW64; rv:2.0.1) Gecko/20100101 Firefox/4.0.1

In a hierarchy of classes the base class has to have all KnownTypeAttribute of the derived and not olny the direct derived.

In this example code, the second serialization throw an SerializationException of type unexpected, but in .Net all run correctly.

class Program
{
	static void Main()
	{
		using (var mem = new MemoryStream())
		{
			BaseClass data = new DerivedA1 { Code = "1", CodeA = "A", CodeA1 = "A1" };
			Serialize(data, mem);
			mem.Position = 0;
			var docResult = Deserialize<BaseClass>(mem);
		}

		using (var mem = new MemoryStream())
		{
			BaseClass data = new DerivedA2 { Code = "1", CodeA = "A", CodeA2 = "A1" };
			
			///////////////////////////////////////////////
			// The next serialization throw an SerializationException
			///////////////////////////////////////////////
			Serialize(data, mem);
			
			
			mem.Position = 0;
			var docResult = Deserialize<BaseClass>(mem);
		}
	}


	public static void Serialize<T>(T instance, Stream destinationStream)
	{
		var serializer = new DataContractSerializer(typeof(T), null, int.MaxValue, false, true, null);

		using (var writer = XmlDictionaryWriter.CreateBinaryWriter(destinationStream, null, null, false))
		{
			serializer.WriteObject(writer, instance);
		}
	}


	public static T Deserialize<T>(Stream sourceStream)
	{
		var serializer = new DataContractSerializer(typeof(T), null, int.MaxValue, false, true, null);

		using (var reader = XmlDictionaryReader.CreateBinaryReader(sourceStream, XmlDictionaryReaderQuotas.Max))
		{
			return (T)serializer.ReadObject(reader);
		}
	}
}

[DataContract]
[KnownType(typeof(DerivedA1))]
[KnownType(typeof(DerivedA))]
public abstract class BaseClass
{
	[DataMember]
	public string Code { get; set; }
}


[DataContract]
[KnownType(typeof(DerivedA1))]
[KnownType(typeof(DerivedA2))]
public abstract class DerivedA : BaseClass
{
	public string CodeA { get; set; }
}

[DataContract]
public class DerivedA1 : DerivedA
{
	[DataMember]
	public string CodeA1 { get; set; }
}

[DataContract]
public class DerivedA2 : DerivedA
{
	[DataMember]
	public string CodeA2 { get; set; }
}

Reproducible: Always

Steps to Reproduce:
1. Create an Application Console with the code of the Details targeting .Net4
2. Run the application
3. the second serialization throw the exception.
Actual Results:  
System.Runtime.Serialization.SerializationException has been thrown
"Type 'MyNameSpace.DerivedA2' is unexpected. The type should either be registered as known type, or DataContractResolver should be used."

Stack Trace:
  at System.Runtime.Serialization.DataContractSerializer.WriteStartObject (System.Xml.XmlDictionaryWriter writer, System.Object graph) [0x00137] in /usr/src/packages/BUILD/mono-2.10.2/mcs/class/System.Runtime.Serialization/System.Runtime.Serialization/DataContractSerializer.cs:463 
  at System.Runtime.Serialization.XmlObjectSerializer.WriteObject (System.Xml.XmlDictionaryWriter writer, System.Object graph) [0x00000] in /usr/src/packages/BUILD/mono-2.10.2/mcs/class/System.Runtime.Serialization/System.Runtime.Serialization/XmlObjectSerializer.cs:106 
  at MyNameSpace.Program.Serialize[BaseClass] (MyNameSpace.BaseClass instance, System.IO.Stream destinationStream) [0x00023] in /home/xxxx/Projects/MyNameSpace/MyNameSpace/Program.cs:38 
  at MyNameSpace.Program.Main () [0x0008d] in /home/xxxx/Projects/MyNameSpace/MyNameSpace/Program.cs:25 

Expected Results:  
None exception.

Mono update to last 10.2 stable.