Nested DataContract serialization exception WCF

1.9k views Asked by At

I have the following DataContracts which want to serialize to:

[DataContract]
public class SwepubHeader
{
    [DataMember(Name = "xsearch")]
    public SwepubBody Body { get; set; }
}

[KnownType(typeof(SwepubSearchItem))]
[DataContract]
public class SwepubBody
{
    [DataMember(Name = "from")]
    public int From { get; set; }

    [DataMember(Name = "to")]
    public int To { get; set; }

    [DataMember(Name = "records")]
    public int Records { get; set; }

    [DataMember(Name = "list")]
    public SwepubSearchItem[] SearchItems { get; set; }
}
[DataContract]
public class SwepubSearchItem
{
    [DataMember(Name = "isbn")]
    public object ISBN { get; set; }

    [DataMember(Name = "title")]
    public object Title { get; set; }

    [DataMember(Name = "description")]
    public object Description { get; set; }

    [DataMember(Name = "identifier")]
    public object Identifier { get; set; }

    [DataMember(Name = "type")]
    public object Type { get; set; }

    [DataMember(Name = "publisher")]
    public object Publisher { get; set; }

    [DataMember(Name = "date")]
    public object Date { get; set; }

    [DataMember(Name = "language")]
    public object Language { get; set; }

    [DataMember(Name = "relation")]
    public object Relation { get; set; }

    [DataMember(Name = "subject")]
    public object Subjects { get; set; }

    [DataMember(Name = "creator")]
    public object Creators { get; set; }
}

When when i try to return this via WCF i get the following exception:

There was an error while trying to serialize parameter http://tempuri.org/:DoSwepubSearchAdvancedResult. The InnerException message was 'Type 'System.Object[]' with data contract name 'ArrayOfanyType:http://schemas.microsoft.com/2003/10/Serialization/Arrays' is not expected. Consider using a DataContractResolver or 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.

I understand i probobly need to do something with KnownTypeAttribute but i havn't really grasped how it is supposed to work. I tried adding SwepubSearchItem as a knowtype of Swepub body but that didn't work. Notworthy is that if i would comment out the SearchItems property of the SwepubBody DataContract the code would work but I wouldnt get the most important part of the response :)

Here is an example of the json that's being serialized: http://libris.kb.se/xsearch?query=barn+design+subject:(utveckling)&format=json&format_level=full&database=swepub

1

There are 1 answers

1
Gabriel Boya On

Have you tried using [KnownType(typeof(SwepubSearchItem[]))] (that is, an array of SwepubSearchItem) to decorate your SwepubBody class ?

However, I would first try to serialize a single SwepubSearchItem, to ensure this class has no issue regarding its own serialization.