A WCF serialization exception is thrown for RS element when reusing AnalyticsEvent contract in WCF Service.

Apr 21, 2010 at 8:40 AM
Edited Apr 21, 2010 at 11:11 AM

WCF cannot serialize Size object by use of System.Drawing implementation. WCF Service throws following exception when using conditional compilation for Microsoft.WebAnalyticsAnalyticsEvent class.

System.Runtime.Serialization.SerializationException, mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089
Error in line 1 position 571. 'EndElement' 'RS' from namespace 'http://schemas.microsoft.com/webanalytics/2010' is not expected. Expecting element 'height'.

If I comment conditional compilation and use the Silverlight version of Size derived from System.Windows then the exception will no longer be thrown by WCF REST service.

 namespace Microsoft.WebAnalytics
{
    using System;
    using System.Collections.ObjectModel;
#if !SILVERLIGHT
   //using System.Drawing;
  using System.Windows;

#endif
    using System.Runtime.Serialization;
#if SILVERLIGHT
    using System.Windows;
    using System.Windows.Media;
#endif
    using Microsoft.WebAnalytics.Data;
    

    /// <summary>
    /// A web analytics event
    /// </summary>
    [DataContract(Name = "AE", Namespace = "http://schemas.microsoft.com/webanalytics/2010")]
    public class AnalyticsEvent

See the differences in Schema when deriving the Size from different implementation. The differences is that these classes uses different casing and types for height and width elements. 

from System.Drawing
<xs:schema xmlns:tns="http://schemas.datacontract.org/2004/07/System.Drawing" elementFormDefault="qualified" targetNamespace="http://schemas.datacontract.org/2004/07/System.Drawing" xmlns:xs="http://www.w3.org/2001/XMLSchema">
  <xs:import namespace="http://schemas.microsoft.com/2003/10/Serialization/" />
  <xs:complexType name="Size">
    <xs:annotation>
      <xs:appinfo>
        <IsValueType xmlns="http://schemas.microsoft.com/2003/10/Serialization/">true</IsValueType>
      </xs:appinfo>
    </xs:annotation>
    <xs:sequence>
      <xs:element name="height" type="xs:int" />
      <xs:element name="width" type="xs:int" />
    </xs:sequence>
  </xs:complexType>
  <xs:element name="Size" nillable="true" type="tns:Size" />
</xs:schema>

From System.Windows
<xs:schema xmlns:tns="http://schemas.datacontract.org/2004/07/System.Windows" elementFormDefault="qualified" targetNamespace="http://schemas.datacontract.org/2004/07/System.Windows" xmlns:xs="http://www.w3.org/2001/XMLSchema">
  <xs:import namespace="http://schemas.microsoft.com/2003/10/Serialization/" />
  <xs:complexType name="Size">
    <xs:annotation>
      <xs:appinfo>
        <IsValueType xmlns="http://schemas.microsoft.com/2003/10/Serialization/">true</IsValueType>
      </xs:appinfo>
    </xs:annotation>
    <xs:sequence>
      <xs:element minOccurs="0" name="Height" type="xs:double" />
      <xs:element minOccurs="0" name="Width" type="xs:double" />
    </xs:sequence>
  </xs:complexType>
  <xs:element name="Size" nillable="true" type="tns:Size" />
</xs:schema>

I already created workitem for this. See the [workitem:12820]
http://msaf.codeplex.com/WorkItem/View.aspx?WorkItemId=12820