views:

275

answers:

1

Im having problems when passing an generic List, trough a WCF operation. In this case, there is a List of int. The example 4 is described here in MSDN. Note that in MSDN sample, is described:

// This will serialize and deserialize successfully because the generic List is equivalent to int[], which was added to known types.

Above, is the DataContract:

    [DataContract]
    [KnownType(typeof(int[]))]
    [KnownType(typeof(object[]))]
    public class AccountData
    {
        [DataMember]
        public object accNumber1;

        [DataMember]
        public object accNumber2;

        [DataMember]
        public object accNumber3;

        [DataMember]
        public object accNumber4;


    }

In client side, Im calling the operation like this:

DataTransfer.Service.AccountData data = new DataTransfer.Service.AccountData()
{
  accNumber1 = 100,
  accNumber2 = new int[100],
  accNumber3 = new List<int>(),
  accNumber4 = new ArrayList()
};
cService.AddAccounts(data);

Also, here is the decorations of the generated AccountData obj (WCF proxy):

[System.Diagnostics.DebuggerStepThroughAttribute()]
[System.CodeDom.Compiler.GeneratedCodeAttribute("System.Runtime.Serialization", "3.0.0.0")]
[System.Runtime.Serialization.DataContractAttribute(Name="AccountData", Namespace="http://schemas.datacontract.org/2004/07/DataTransfer.Service")]
[System.SerializableAttribute()]
[System.Runtime.Serialization.KnownTypeAttribute(typeof(DataTransfer.Client.CustomerServiceReference.PurchaseOrder))]
[System.Runtime.Serialization.KnownTypeAttribute(typeof(DataTransfer.Client.CustomerServiceReference.Customer))]
[System.Runtime.Serialization.KnownTypeAttribute(typeof(int[]))]
[System.Runtime.Serialization.KnownTypeAttribute(typeof(object[]))]

The exception is:

There was an error while trying to serialize parameter http://tempuri.org/:myEntity. The InnerException message was 'Type Generic List' with data contract name 'ArrayOfint:http://schemas.microsoft.com/2003/10/Serialization/Arrays' is not expected. Add any types not known statically to the list of known types

A: 

If you would declare your type like this, then serialization works fine:

[DataContract]   
public class AccountData
{
    [DataMember]
    public object accNumber1 {get; set;}

    [DataMember]
    public int[] accNumber2 { get; set; }

    [DataMember]
    public List<int> accNumber3 { get; set; }

    [DataMember]
    public ArrayList accNumber4 {get; set;}

}

(I recommend to use properties instead of public fields.)

Do you really need your fields to be of type object? If the above class is too restrictive then there are ways to make it more flexible, but maybe not as flexible as you intended.

Also note that the KnownType attribute applies to the entire class and not individual properties.

Stefan Egli
I were doing tests with KnownTypeAttribute and studying the MSDN example (that's why the object prop). I will try your suggestion. Thanks.
Erup
@downvoter: what is wrong with my answer?
Stefan Egli