locked
Serialization problem. RRS feed

  • Question

  • I have a method that when called by a client gets:

     

    System.ServiceModel.CommunicationException: The underlying secure session has faulted before the reliable session fully completed. The reliable session was faulted.

     

    When I look at the trace log I see:

     

    <ExceptionType>System.ServiceModel.CommunicationException, System.ServiceModel, Version=3.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089</ExceptionType>
    <Message>There was an error while trying to serialize parameter
    http://Buyseasons.WebServices.BsiServices.ServiceContracts/2006/12:GetBsiServicesDataMessageResult. The InnerException message was 'Type 'BuySeasons.WebServices.ServiceCommon.Vendor.Amazon.ToysBaby' with data contract name 'ToysBaby:http://schemas.datacontract.org/2004/07/BuySeasons.WebServices.ServiceCommon.Vendor.Amazon' is not expected. Add any types not known statically to the list of known types - for example, by using the KnownTypeAttribute attribute or by adding them to the list of known types passed to DataContractSerializer.'.  Please see InnerException for more details.</Message>

     

    The type that it is complaining about is defined in an XSD that I have no desire to modify as it comes from a third-party. The class that is being serialized is generated usiing the XSD tool. Here is a sample of the output:

     

    /// <remarks/>

    [System.CodeDom.Compiler.GeneratedCodeAttribute("xsd", "2.0.50727.42")]

    [System.SerializableAttribute()]

    [System.Diagnostics.DebuggerStepThroughAttribute()]

    [System.ComponentModel.DesignerCategoryAttribute("code")]

    [System.Xml.Serialization.XmlTypeAttribute(AnonymousType=true)]

    public partial class ProductProductData {

    private object itemField;

    /// <remarks/>

    [System.Xml.Serialization.XmlElementAttribute("AutoAccessory", typeof(AutoAccessory))]

    . . . .

    [System.Xml.Serialization.XmlElementAttribute("ToysBaby", typeof(ToysBaby))]

    . . .

    public object Item {

    get {

    return this.itemField;

    }

    set {

    this.itemField = value;

    }

    }

    }

     

    So although it is not explicitly using the KnownTypes attribute, I am assuming that the XmlElement attribute generated by XSD would have the same effect. Are classes generated by the XSD tool usable from WCF? I understood that if it is marked [Serializable] that the class could be used in WCF. Am I wrong? Any other ideas on what is wrong?

     

    Thank you.

     

    Kevin

    Tuesday, June 26, 2007 10:48 AM

Answers

  • Types generated by the XSD tool are serializable by the Xml Serializer.  If you know more about the Product type, you may be able to hook up a DataContractSurrogate in this case and map the missing fields to the appropriate DataMembers.  Take a look at this sample for guidance:

    http://msdn2.microsoft.com/en-us/library/ms751540.aspx

     

    I really hope this helps.  I know I addressed the other issue (about faults) in another thread, so I am hopeful that you will be able to use DataContractSurrogate here to ensure use of the DataContractSerializer as opposed to the Xml Serializer.

     

    -- Dave

    Tuesday, June 26, 2007 10:51 PM

All replies

  • Looking at the comments generated by svcutil it recommends using the XmlSerializerFormatAttribute. I only want Xml serialization for this one method. Can I mix the serialization modes? The documentation seems to indicate that this attribute should be applied to the service contract which would mean every method would use the non-default DataContract serialization method. If my contract looks like (for just this method):

     

    [DataContract()]

    public sealed class AmazonProductFeedResponseType

    {

    private Product[] _ProductFields;

    [DataMember(IsRequired = true)]

    public Product[] Products

    {

    get

    {

    return this._ProductFields;

    }

    set

    {

    this._ProductFields = value;

    }

    }

    }

     

    How can it be modified so that this type (which was generated by the XSD tool) can be serialized? Or is this possible?

     

    Thanks again.

     

    Kevin

    Tuesday, June 26, 2007 11:13 AM
  • Types generated by the XSD tool are serializable by the Xml Serializer.  If you know more about the Product type, you may be able to hook up a DataContractSurrogate in this case and map the missing fields to the appropriate DataMembers.  Take a look at this sample for guidance:

    http://msdn2.microsoft.com/en-us/library/ms751540.aspx

     

    I really hope this helps.  I know I addressed the other issue (about faults) in another thread, so I am hopeful that you will be able to use DataContractSurrogate here to ensure use of the DataContractSerializer as opposed to the Xml Serializer.

     

    -- Dave

    Tuesday, June 26, 2007 10:51 PM