none
Serialize a DataSet using the DataContractSerializer with a surrogate? RRS feed

  • Question

  • I have an OperationContract that returns a DataSet.  Before opening my host, I have set the data contract serializer on each OperationDescription to use my surrogate:

    Code Snippet

    DataContractSerializerOperationBehavior existingBehavior = operation.Behaviors.Find ();
            existingBehavior.DataContractSurrogate = surrogate;

     

     

    This behavior works fine on every other operation.  However, if my DataSet contains a DataContract element that has a DataMember which is a KnownType for my surrogate, it doesn't get serialized.  After my initial research, I thought that maybe the response was using the XmlSerializer.  I was finally able to capture the XML being returned, though, and it seems like it is using the DataContractSerializer:

     

    Code Snippet
    <DataContractObject type="System.Data.DataSet, System.Data, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" xmlns="http://www.empty.com/schemas/MySerializable.1.0">
        <DataSet xmlns="">
            <xs:schema id="NewDataSet" xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns:msdata="urn:schemas-microsoft-com:xml-msdata">
                <xs:element name="NewDataSet" msdata:IsDataSet="true" msdata:UseCurrentLocale="true">
                    <xs:complexType>

     

     

     

    However, looking at the stack trace leading up to the point where my DataContract object is being serialized shows that it is using the XmlSerializer.


       at Microsoft.Xml.Serialization.GeneratedAssembly.XmlSerializationWriterNavNode.Write3_NavNode(String n, String ns, NavNode o, Boolean isNullable, Boolean needType)
       at Microsoft.Xml.Serialization.GeneratedAssembly.XmlSerializationWriterNavNode.Write4_NavNode(Object o)
       at Microsoft.Xml.Serialization.GeneratedAssembly.NavNodeSerializer.Serialize(Object objectToSerialize, XmlSerializationWriter writer)
       at System.Xml.Serialization.XmlSerializer.Serialize(XmlWriter xmlWriter, Object o, XmlSerializerNamespaces namespaces, String encodingStyle, String id)
       at System.Xml.Serialization.XmlSerializer.Serialize(XmlWriter xmlWriter, Object o)
       at System.Data.Common.ObjectStorage.ConvertObjectToXml(Object value, XmlWriter xmlWriter, XmlRootAttribute xmlAttrib)
       at System.Data.XmlDataTreeWriter.XmlDataRowWriter(DataRow row, String encodedTableName)
       at System.Data.XmlDataTreeWriter.SaveDiffgramData(XmlWriter xw, Hashtable rowsOrder)
       at System.Data.NewDiffgramGen.Save(XmlWriter xmlw, DataTable table)
       at System.Data.DataSet.WriteXml(XmlWriter writer, XmlWriteMode mode)
       at System.Data.DataSet.System.Xml.Serialization.IXmlSerializable.WriteXml(XmlWriter writer)
       at System.Runtime.Serialization.XmlObjectSerializerWriteContext.WriteIXmlSerializable(XmlWriterDelegator xmlWriter, Object obj, XmlSerializableWriter xmlSerializableWriter)
       at System.Runtime.Serialization.XmlDataContract.WriteXmlValue(XmlWriterDelegator xmlWriter, Object obj, XmlObjectSerializerWriteContext context)
       at System.Runtime.Serialization.DataContractSerializer.InternalWriteObjectContent(XmlWriterDelegator writer, Object graph)
       at System.Runtime.Serialization.DataContractSerializer.InternalWriteObject(XmlWriterDelegator writer, Object graph)
       at System.Runtime.Serialization.XmlObjectSerializer.WriteObjectHandleExceptions(XmlWriterDelegator writer, Object graph)
       at System.Runtime.Serialization.DataContractSerializer.WriteObject(XmlWriter

     

    I have seen other threads and MSDN articles mentioning that unless you use a certain switch with svcutil, DataSets will be serialized with the XmlSerializer.  I'm not using svcutil to generate my proxies.  This is an in-house application so both client and server share a library with the contract definition.  Is there any way to force this operation that returns a DataSet to use a DataContractSerializer with my surrogate?

     

    Thanks,
    Carole

    Monday, August 4, 2008 1:26 PM