Search code examples
c#wcfdatacontractdatamember

Error when data contract member refer each other


i have a simple data contract which is have a data member that refer to each other, here are the data member:

 [DataContract(Namespace = "", Name = "ScaleTransactionHeaderMessage")]
public class ScaleTransactionHeaderMessage
{
    [DataMember]
    public int ScaleTransactionHeaderMessageId { get; set; }

    [DataMember]
    public string OperatorName { get; set; }

    [DataMember]
    public string Shift { get; set; }

    [DataMember]
    public string Source { get; set; }

    [DataMember]
    public string Destination { get; set; }

    **[DataMember]
    public List<ScaleTransactionDetailMessage> ScaleTransactionDetailMessages { get; set; }**

}

[DataContract(Namespace = "", Name = "ScaleTransactionDetailMessage")]
public class ScaleTransactionDetailMessage
{

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

    [DataMember]
    public double Tonnage { get; set; }

    [DataMember]
    public DateTime TransactionDetailDate { get; set; }

    **[DataMember]
    public ScaleTransactionHeaderMessage scaleTransactionHeaderMessage { get; set; }**

}

Here is the operation causing the problem

        private static ScaleTransactionDetailMessage ConvertTransactionDetail(ScaleTransactionHeaderMessage headerMessage, ScaleTransactionDetail transactionDetail)
    {
        ScaleTransactionDetailMessage detailMessage = new ScaleTransactionDetailMessage
        {
            Tonnage = transactionDetail.Tonnage,
            TransactionDetailDate = transactionDetail.TransactionDetailDate,
            ScaleTransactionDetailMessageId = transactionDetail.TransactionDetailId,
           //TODO: Check why this is not working
            **scaleTransactionHeaderMessage = headerMessage**
        };
        return detailMessage;
    }

The problem is every time i add ScaleTransactionHeaderMessage in the ScaleTransactionDetailMessage data contract i always got an error mentioning connection timeout, i'm sure this is not configuration issue since if i did not add the value to the ScaleTransactionHeaderMessage in the operation contract the service can running properly.

i have unit test the operation and it is working properly, the problem only appear when invoking the service.

Is there any mistakes in the data contract design ?


Solution

  • You need to add IsReference = true to the DataContract:

    [DataContract(IsReference=true]
    

    Take a look here.