Mark Conway

Mark Conway

  • NA
  • 10
  • 16.5k

InvalidDataContractException occurs when returning Response Class that contains IEnumerable

Jun 7 2011 2:09 PM
Hi there,

I am encountering an exception when my WCF service attempts to create the data contract response class.

I thought that this problem might be have resulted from WCF not knowing how to Serialize IEnumberable, so I marked it as a known type. This did not work.

Any help on this would be quite welcome!!!

Details below.

Many thanks,

Mark

Service Contract:

   [ServiceContract]
    public interface IRefundService
    {
        [OperationContract]
        RefundSearchCriteriaResponse FindRefunds(RefundSearchCriteriaRequest refundSearchCriteriaRequest);
     }

Data Contract:

    [DataContract]
    [KnownType(typeof(List<Refund>))]
    public class RefundSearchCriteriaResponse
    {


        [DataMember]
        public IEnumerable<Refund> Refunds { get; set; }


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

Exception Details:
ExceptionInvalidDataContractException
Message: Type 'System.Linq.OrderedEnumerable`2[Asos.Operations.Finance.ElectronicFunds.Entity.Refund,System.Nullable`1[System.Int64]]' cannot be serialized. Consider marking it with the DataContractAttribute attribute, and marking all of its members you want serialized with the DataMemberAttribute attribute.  See the Microsoft .NET Framework documentation for other supported types.


Call stack:
System.Runtime.Serialization.dll!System.Runtime.Serialization.DataContract.DataContractCriticalHelper.ThrowInvalidDataContractException(string message, System.Type type) + 0x12a bytes
------------------------------------------------------------------------------------------
 System.Runtime.Serialization.dll!System.Runtime.Serialization.DataContract.DataContractCriticalHelper.CreateDataContract(int id, System.RuntimeTypeHandle typeHandle, System.Type type = {Name = "OrderedEnumerable`2" FullName = "System.Linq.OrderedEnumerable`2[[Asos.Operations.Finance.ElectronicFunds.Entity.Refund, Asos.Operations.Entity, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null],[System.Nullable`1[[System.Int64, mscorlib, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089]], mscorlib, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089]]"}) + 0x20f bytes
------------------------------------------------------------------------------------------
System.Runtime.Serialization.dll!System.Runtime.Serialization.DataContract.DataContractCriticalHelper.GetDataContractSkipValidation(int id = 28, System.RuntimeTypeHandle typeHandle, System.Type type) + 0x4b bytes



Answers (3)