Home Contact

Allan Rwakatungu's blog

Developer and Enterprise Architect

News

Twitter












Archives

Post Categories

Image Galleries

Syndication:

Serializing an abstract data contract

Recently I needed to serialize abstract classes in WCF.

The best way to explain it is with an example.

I have a abstract class

[KnownType(typeof(CreditCardPayment))]
public abstract class Payment
{
    private string _amount;
    private string _currency;
    [DataMember]
    public string currency
    {
        get { return _currency; }
        set { _currency = value; }
    }
    [DataMember]
    public string amount
    {
        get { return _amount; }
        set { _amount = value; }
    }
 
}

The class deriving it is

[DataContract]
public class CreditCardPayment:Payment
{
    private string _creditCardNumber;
    private DateTime _expiryDate;
    private string _securityCode;
    [DataMember]
    public string securityCode
    {
        get { return _securityCode; }
        set { _securityCode = value; }
    }
    [DataMember]
    public DateTime expiryDate
    {
        get { return _expiryDate; }
        set { _expiryDate = value; }
    }
 
    [DataMember]
    public string creditCardNumber
    {
        get { return _creditCardNumber; }
        set { _creditCardNumber = value; }
    }
 
}

When I attempted to serialize this to a message queue, I got the error below

at Microsoft.Xml.Serialization.GeneratedAssembly.XmlSerializationWriterAirTimeOrder.Write3_Payment(String n, String ns, Payment o, Boolean isNullable, Boolean needType)
   at Microsoft.Xml.Serialization.GeneratedAssembly.XmlSerializationWriterAirTimeOrder.Write5_AirTimeOrder(String n, String ns, AirTimeOrder o, Boolean isNullable, Boolean needType)
   at Microsoft.Xml.Serialization.GeneratedAssembly.XmlSerializationWriterAirTimeOrder.Write6_AirTimeOrder(Object o)


The solution is to the code below

[DataContract(Namespace = "www.pro.com")]
[KnownType(typeof(CreditCardPayment))]

[System.Xml.Serialization.XmlInclude(typeof(CreditCardPayment))]
public abstract class Payment
{
    private string _amount;
    private string _currency;
    [DataMember]
    public string currency
    {
        get { return _currency; }
        set { _currency = value; }
    }
-------------------------------------------------------------------------------


Tuesday, May 27, 2008 1:59 PM

Feedback

# re: Serializing an abstract data contract

Ah nice example, i don't know what to say except that I have enjoyed reading what you all have to say...
2/13/2011 10:13 AM | free credit score

# re: Serializing an abstract data contract

Thank you so much pal! This thing was driving me crazy, cause when debugging everything was fine.
The problem was under the serialization.
Good tip. 6/3/2011 4:46 PM | Mauricio Leyzaola

# re: Serializing an abstract data contract

Hey I got the same problem but I ended up in adding the known type on the parent only but not on child which worked... I didnt add the xmlInclude attribute 4/26/2012 8:45 AM | Srujan

Post A Comment
Title:
Name:
Email:
Website:
Comment:
Verification: