DataContract Known Types - Walkthrough Array
I am having problems passing a shared list through a WCF operation. In this case, there is a list of int. Example 4 is described here on MSDN . Please note the MSDN example describes:
// This will serialize and deserialize successfully because the generic list is equivalent to int [] that has been added to the known types.
Above, this is 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;
}
On the 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 are 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:
An error occurred while trying to serialize the parameter http://tempuri.org/:myEntity . The InnerException message was "Shared List Type" with the 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 source to share
If you declare your type like this, then serialization is 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 using properties instead of public fields.)
Do you really need your fields for the object type? If the above class is too restrictive, there are ways to make it more flexible, but perhaps not as flexible as you intended.
Also note that the attribute KnownType
applies to the entire class and not to individual properties.
a source to share