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:          }
Published Wednesday, December 17, 2008 3:35 PM by gsusx

Comments

Wednesday, December 17, 2008 4:05 PM by Using XAML serialization in WCF 4.0 - Jesus Rodriguez's WebLog

# Using XAML serialization in WCF 4.0 - Jesus Rodriguez's WebLog

Pingback from  Using XAML serialization in WCF 4.0 - Jesus Rodriguez's WebLog

Wednesday, December 17, 2008 4:06 PM by externsblogs

# Using XAML serialization in WCF 4.0

Declarative Services is one of the exciting new features of Windows Communication Foundation (WCF) 4

Thursday, December 18, 2008 11:11 AM by Scott Prugh

# re: Using XAML serialization in WCF 4.0

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.

Thursday, January 01, 2009 11:29 PM by Jay R. Wren

# re: Using XAML serialization in WCF 4.0

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

Wednesday, February 18, 2009 3:31 PM by Pablo M. Cibraro (aka Cibrax)

# Contract Projections in WCF declarative services

As my friend Jesus mentioned in the post " Using XAML serialization in WCF 4.0 ", WCF 4.0 introduces

Friday, May 08, 2009 7:38 PM by Community Blogs

# What’s New in WCF 4.0?

What’s New in WCF 4.0? בתקופה האחרונה אני משקיע הרבה זמן על ללמוד את החידושים&#160; בדוט-נט 4.0 ובפרט

Leave a Comment

(required) 
(required) 
(optional)
(required)