Property of type List<Object> in DataContract class fails

2.8k Views Asked by At

I'm developing a WCF with two Classes as DataContracts. One of them is a Data Structure developed by myself which manages Objects as JSON one, it names JSON; the other is just a Customized Object that my WebService recieve, it names Emission. I have three methods; one is for create policies, other one retrieves a policy and the last one consults catalogs in a dynamical way using JSON class. My problem comes within an error message like this:

"The formatter threw an exception while trying to deserialize the message: There was an error while trying to deserialize parameter ... The InnerException message was 'Error in line 1 position 823. Element ... contains data of the 'http://schemas.microsoft.com/2003/10/Serialization/Arrays:ArrayOfanyType' data contract. The deserializer has no knowledge of any type that maps to this contract. Add the type corresponding to 'ArrayOfanyType' to the list of known types - for example, by using the KnownTypeAttribute attribute or by adding it to the list of known types passed to DataContractSerializer.'.
Please see InnerException for more details."

I was looking for an answer then I noticed that the cause of my problem was a property in my JSON class which is a list of generic Objects. I need this property in my client to initialize the object I expect to recieve, so this list could contain strings or another List of Objects that's why I need this kind of item.

I've tried to use sort of KnownTypes with no success but I don't know if I'm doing something wrong

[KnownType(typeof(Object[]))]
[KnownType(typeof(List<List<Object>>))]
[KnownType(typeof(List<object>))]
[KnownType(typeof(List<string>))]
[KnownType(typeof(List<List<string>>))] 

It is important to say that if I SET value property as internal everything goes fine even JSON class in client side although value property is never shown. Attach a shred of my code:

public class JSON
{
    #region
    [DataMember]
    public List<Object> value { get; set; }  This cause the problem
    //public List<Object> value { get; internal set; }  This allow everything happens fine
    [DataMember]
    public List<string> errors { get; set; }
    [DataMember]
    public Regex pattern { get; internal set; }
    [DataMember]
    internal Regex commaPattern { get; private set; }
    #endregion
}

[ServiceContract(Namespace = "http://EmissionService")]
public interface IEmissionService
{
    [OperationContract]
    [WebGet(UriTemplate = "Emissions/getCatalog", ResponseFormat = WebMessageFormat.Json)]
    string getCatalog(JSON request);

    [OperationContract]
    [WebInvoke(UriTemplate = "Emissions/createPolicy", ResponseFormat = WebMessageFormat.Json, Method = "POST")]
    string createPolicy(Emission emissionRequest);

    [OperationContract]
    [WebGet(UriTemplate = "Emissions/getPolicy", ResponseFormat = WebMessageFormat.Json)]
    JSON getPolicy(JSON request);
}

I hope you can really help me. Thanks in advice!

2

There are 2 best solutions below

0
Zinthos On

A possible wrap around could be using an array rather than a list in your data contracts and use ToArray() and ToList() appropriately in your service.

0
cederlof On

All types that the object-list can contain must be known by the compiler for the deserialization to work.