This post is part of a series about WCF extensibility points. For a list of all previous posts and planned future ones, go to the index page.
Wrapping up the “chapter” on serialization, this post will talk about other extensions for the serialization features in WCF which are too small to deserve their own post.
IExtensibleDataObject is all about versioning. Its main (sole?) purpose is round-tripping different versions of a data contract without losing information. Take a Person type, for instance, with the definition as shown below.
This type is used in a service which has many methods to store and retrieve this information used by clients. One client application, for instance, is responsible for birthdays, adding 1 to the person’s age and returning it back to the server. Everything works out fine.
Now there is a change in the business logic of the server (or the database schema), where the person object will also hold a reference to the address, as follows.
Old clients (with the original contract) still work out fine – the contract name and namespace match, the fields are in the expected order (remember, the DataContractSerializer enforces the order of the fields), and the old clients will simply ignore the extra element for the address which comes from the updated server. And new client applications can take advantage of this new field, and we can have a new app which can be used to update the client address. But the fact that the old clients ignore the address, while not a problem for themselves, can pose a problem for the system as a whole. Coming back to the client which knows about birthdays, if the client retrieves an object from the server with an Address value, it will discard that value. After it updates the person’s age and sends the Person instance back to the server, it will not have an Address field, and so it will be received as null (Nothing) in the server side.
That’s where the IExtensibleDataObject interface (IEDO) comes into play. By declaring a type as extensible (implementing the IEDO interface), the type provides to WCF a place to store any members which it doesn’t know about. When the object is reserialized, those “unknown” members are serialized back (in the same order they were received). By default, all data contract types generated using svcutil.exe or Add Service Reference implement that interface, so those clients are versioning-safe (at least with respect to additions in the data contract).
Implementing IEDO is trivial: just one property of type ExtensionDataObject – all the type needs to do is to store and return this object. ExtensionDataObject is an opaque type: it has no public properties (so you can’t set or retrieve the additional data), it’s sealed (you can’t extend it) and it doesn’t have any public constructors (you can’t create an instance of it) – it can only be manipulated by the WCF serializer internally. Here’s the same Person class, this time versioning-aware. The automatic properties in C# makes its implementation quite simple – it’s literally a one-line change addition plus the declaration that the class implements IEDO.
And that’s likely all you’ve ever need to know about IExtensibleDataObject.
The serialization callback attributes were introduced in .NET Framework 2.0, but since the first version there have been serializers (such as the BinaryFormatter and the SoapFormatter). In that version, there was one option for running code after the deserialization of the object graph was completed, and that was with the IDeserializationCallback interface. On .NET Framework 2.0, I imagine the designers thought that adding three more interfaces would make the class declaration overly complex, and decided to go with the attribute approach instead (this is just my theory).
IDeserializationCallback (IDC) is also a simple interface, with a single method, OnDeserialization, which is run when the entire object graph has been deserialized. It’s almost equivalent to the OnDeserializedAttribute, in a way that the callback method declared with that attribute will be invoked for an object after that object has finished deserializing its members. Take the example below, in which a Person class has two members of type Address. When we deserialize an instance using the BinaryFormatter, first the Person.OnDeserializing is called, indicating that that type is about to be deserialized. Then, the first Address.OnDeserializing is invoked, that object is deserialized (read from the stream), and Address.OnDeserialized is called for the first address. The same happens for the second address in the person: Address.OnDeserialzing, the second object is read from the stream, then Address.OnDeserialized is called. Finally, since all the members of Person have been read from the stream, Person.OnDeserialized is called. And now that the entire deserialization episode has finished, the IDeserializationCallback.OnDeserialization method is invoked on the Person object and in both Address instances.
Now, this is the behavior on the Binary (and Soap) Formatter classes. The IDeserializationCallback interface is also supported on the WCF serializers (DataContractSerializer, NetDataContractSerializer and DataContractJsonSerializer), but its behavior is different than on the original formatters (I have no idea why). On the WCF serializers, IDeserializationCallback.OnDeserialization is essentially equivalent to OnDeserializedAttribute, in which it’s invoked when the object is deserialized. Possibly because of this, I’ve never seen IDeserializationCallback being used in WCF, with the serialization callback attributes being used instead. And the rhetorical question: if both are used, then IDC is invoked first, then OnDeserialized (see in the code in this post for examples) on the WCF serializers, but really, just don’t use both to avoid unnecessary complications :-).
Another little-used serialization extensibility, IObjectReference (IOR) is an interface which indicates that the class which implements it can return a reference to a different object after it has been deserialized. The main scenario for this interface is for when we want to have a singleton class; since the serializer bypasses the constructor, it can bypass the singleton protection of the class. But by marking the type as IOR, we can, after the serializer created the new object, tell it to use the object which we want. But its usage isn’t limited to singletons – any time we need to restrict the actual instances of a certain type, we can use this interface, as in the example below.
When the object deserialization is done, the serializer will call IObjectReference.GetRealObject, and the class can replace itself in the deserialization graph with another object. In the example above, we’ve “protected” the class by only having private constructors, but since serialization bypasses them, new objects would be created. By implementing IObjectReference, we can maintain the instancing control over the instances of the type.
Final thoughts about serialization extensibility
The serializers in WCF (and the “legacy” ones) have quite a few extensibility points, but in most cases you won’t need to use them. But if the needs is there, I hope those posts will help you to understand your options for solving those issues.