Search icon CANCEL
Arrow left icon
Explore Products
Best Sellers
New Releases
Books
Videos
Audiobooks
Learning Hub
Conferences
Free Learning
Arrow right icon
Arrow up icon
GO TO TOP
Microsoft Windows Communication Foundation 4.0 Cookbook for Developing SOA Applications

You're reading from   Microsoft Windows Communication Foundation 4.0 Cookbook for Developing SOA Applications Over 85 easy recipes for managing communication between applications

Arrow left icon
Product type Paperback
Published in Oct 2010
Publisher Packt
ISBN-13 9781849680769
Length 316 pages
Edition 1st Edition
Tools
Arrow right icon
Author (1):
Arrow left icon
Juntao Cheng Juntao Cheng
Author Profile Icon Juntao Cheng
Juntao Cheng
Arrow right icon
View More author details
Toc

Table of Contents (20) Chapters Close

Microsoft Windows Communication Foundation 4.0 Cookbook for Developing SOA Applications
Credits
Foreword
About the Author
About the Reviewers
Preface
1. Working with Contracts 2. Endpoint, Binding, and Behavior FREE CHAPTER 3. Hosting and Configuration 4. Service Discovery and Proxy Generation 5. Channel and Messaging 6. Dealing with Data in Service 7. Security 8. Concurrency 9. Extending WCF Runtime 10. RESTful and AJAX-enabled WCF Services 11. Interoperability 12. Diagnostics 13. Miscellaneous WCF Development Tips Index

Make DataContract forward-compatible


WCF uses a serialization engine called DataContractSerializer by default, to serialize and deserialize data. If we want to add new complex data types (that will be transferred in service operations) in a WCF service, we need to define it as a DataContract type so as to make it friendly to the DataContractSerializer engine. A .NET serialization system supports backward-compatibility on custom data types naturally. However, sometimes we also need forward-compatibility for data types used in a WCF service. Suppose that you have a service that exchanges some custom data types between clients. If one side updates the custom data type (adds some fields or properties) or uses a newer version, it is important to make sure that the other side (without using the updated version of data) can still work correctly with the updated data type instances.

How to do it...

  1. Make the custom data type (we will use in our service communication) implement the IExtensibleDataObject interface.

    [DataContract]
        public class FCQuestion : IExtensibleDataObject
        {
            [DataMember]
            public string Subject { get; set; }
            [DataMember]
            public string Answer { get; set; }
          
            public ExtensionDataObject ExtensionData
            {
                get;
                set;
            }
        }
  2. Make sure you haven't enabled the IgnoreExtensionDataObject property on ServiceBehaviorAttribute applied on your WCF service (this property is disabled by default).

    You can have a look at the article ServiceBehaviorAttribute.IgnoreExtensionDataObject Property for more information and is available at:

    http://msdn.microsoft.com/en-us/library/system.servicemodel.servicebehaviorattribute.ignoreextensiondataobject.aspx

How it works...

After the DataContract type implements the IExtensibleDataObject interface, an ExtensionDataObject property is added; this property plays an important role in forward-compatible serialization. WCF will use DataContractSerializer for DataContract type serialization/deserialization. When DataContractSerializer finds that a certain type (used for operation parameters or return value) has implemented the IExtensibleDataObject interface, it will store any data (this is obtained from the message stream during deserialization) that doesn't have corresponding property/fields in the type definition into the ExtensionDataObject property so that these data will not get lost. And if the deserialized instance (with some unknown data stored in ExtensionDataObject) is serialized into the message later, DataContractSerializer will write out ExtensionDataObject into the message stream again. This ensures that the data in the new version of DataContract can be consumed by the service/client with the old type definition correctly, instead of raising unexpected type, mismatching, or serialization exceptions.

The following modified data type can be consumed by the service/client that has the old definition, as explained earlier, without synchronizing the DataContract type definition:

[DataContract]
    public class FCQuestion : IExtensibleDataObject
    {
        [DataMember]
        public string Subject { get; set; }
        [DataMember]
        public string Answer { get; set; }
        [DataMember]
        public string Comment { get; set; }

        public ExtensionDataObject ExtensionData
        {            get; set;       }
    }

There's more...

Currently, using the IExtensibleDataObject interface can make the DataContractSerializer preserve unknown data properties/fields when deserializing/serializing custom data types. However, the ExtensionDataObject property is an opaque object to developers and we do not have means to manually read the data stored in it. In case we want to manually extract the additional unknown property/fields, we can consider directly accessing the underlying SOAP message via MessageInspector or other extension points.

See also

  • Altering an operation message via MessageInspector in Chapter 9.

  • Complete source code for this recipe can be found in the \Chapter 1\recipe2\ folder

lock icon The rest of the chapter is locked
Register for a free Packt account to unlock a world of extra content!
A free Packt account unlocks extra newsletters, articles, discounted offers, and much more. Start advancing your knowledge today.
Unlock this book and the full library FREE for 7 days
Get unlimited access to 7000+ expert-authored eBooks and videos courses covering every tech area you can think of
Renews at €18.99/month. Cancel anytime