I blog about debugging, development using .net, C#, SQL, and other Microsoft technologies.
Disclaimer: All posts are provided "AS IS" with no warranties, confering no rights, and expressing only my personal opinion, not Microsoft's.
If you use Data Contracts, then probably your classes implement the IExtensibleDataObject interface to allow (de)serialization of different versions of the data contract. I won’t go into details about the use of ExtensionDataObject, the type of the ExtensionData property you need to implement. I just want to point out a possible scenario, using sample code similar to that of MSDN:
1: using System;
2: using System.Runtime.Serialization;
3: using System.Runtime.Serialization.Formatters.Binary;
5: namespace Test
9: public class Person : IExtensibleDataObject
11: private ExtensionDataObject extensionData;
13: public ExtensionDataObject ExtensionData
17: return extensionData;
21: extensionData = value;
26: public string Name;
29: class Program
31: static void Main(string args)
35: new BinaryFormatter().Serialize(Console.OpenStandardOutput(), new Person());
37: catch (Exception ex)
If you serialize an object of the type used in this example, Person, using a BinaryFormatter, you will get the following exception:
System.Runtime.Serialization.SerializationException: Type 'System.Runtime.Serialization.ExtensionDataObject' in Assembly 'System.Runtime.Serialization, Version=188.8.131.52, Culture=neutral, PublicKeyToken=b77a5c561934e089' is not marked as serializable.
The solution is to mark the private extensionData field as non-serialized:
2: private ExtensionDataObject extensionData;
Thanks its very usefully
Thanks, this was what I needed to solve my problem.
Glad it helped :)
But this way you're defeating the purpose of IExtensibleDataObject in the first place...
It would be far better if you would just implement the "On serialize" (constructor with SerializationInfo & StreamingContext arguments) and "On deserialize" (GetObjectData()) methods of ISerializable interface, and handle the binary serialization yourself by manually adding the ExtensionDataObject serialized via DataContract, and the rest with binary serialization. That way you can keep the functionallity of IExtensibleDataObject and binary serialization together, without breaking anything.