Using XAML serialization in WCF 4.0

Declarative Services is one of the exciting new features of Windows Communication Foundation (WCF) 4.0. By declarative, we are referring to services that are completely modeled by using the Extensible Application Markup Language (XAML). As you might think, this capability will open to door for a whole new set of scenarios in Service Oriented systems which are really hard to implement with the current technologies. The specific capabilities of declarative services will be the subject of a future post. Today, I would like to explore one of the features that enable the implementation of declarative services: XAML serialization.

You don’t really need to know a lot about declarative service to figure out that there it must be a component that handles the translation between XAML and CLR types. This is precisely the role of the XamlServices class included in the System.Runtime.Xaml namespace. This class implements the serialization and deserialization process between XAML and .NET types and consequently it’s a key component of the declarative services infrastructure. Although we can currently use different mechanisms for implementing Xaml serialization, XamlServices exposes a programming model considerably simpler than other alternatives.

Let’s take the following class definition.

   1:     public class Contact
   2:      {
   3:          private string firstName;
   4:          private string lastName;
   5:          private DateTime birthDay;
   6:   
   7:          public Contact()
   8:          { }
   9:   
  10:          public string FirstName
  11:          {
  12:              get { return firstName; }
  13:              set { firstName = value; }
  14:          }
  15:   
  16:          public string LastName
  17:          {
  18:              get { return lastName; }
  19:              set { lastName = value; }
  20:          }
  21:   
  22:          public DateTime BirthDay
  23:          {
  24:              get { return birthDay; }
  25:              set { birthDay = value; }
  26:          }
  27:      }

In order to serialize an instance of the Contact class into XAML we can use the XamlServices class as illustrated in the following code.

   1:    private static void XamlSerializationSample()
   2:          {
   3:              Contact contact = new Contact();
   4:              contact.FirstName = "fn";
   5:              contact.LastName = "ln";
   6:              contact.BirthDay = DateTime.Now;
   7:              XmlWriter writer = XmlWriter.Create(file path...);
   8:              XamlServices.Save(writer, contact);
   9:          }

The output of the serialization looks like the following.

   1:  <?xml version="1.0" encoding="utf-8"?>
   2:  <Contact BirthDay="2008-12-16T19:58:14.1173568-08:00" FirstName="fn" LastName="ln" 
                xmlns="clr-namespace:XamlSerialization;assembly=XamlSerialization" />

Additionally, we can use a similar algorithm to deserialize the XAML representation into an instance of the contact class.

   1:    private static void XamlDeserializationSample()
   2:          {
   3:              XmlReader reader= XmlReader.Create(file path...);
   4:              Contact contact= (Contact)XamlServices.Load(reader);
   5:          }

2 Comments

  • Has WCF been updated to support datashaping on the wire based on the XAML serilaizer? Your sample eludes to this.

    I have a few gripes with WCF Contracts and the biggest one is the lack of DataShaping with the DCS and the ability to use attributes like the XmlSerializer supported. If the XAML serializer was supporrted on the wire, we would be closer to this.

  • Sounds like WCF4 will then have support for serializing Directed Acyclic Graph data structures then?

Comments have been disabled for this content.