views:

125

answers:

1

Hi, I am new to HTTP Post and encounter an strange issue. I have a request object defined as below:

[DataContract(Namespace = "http://Test.com/WCF")]
    public class Request
    {
        [DataMember]
        public string CardNumber { get; set; }
        [DataMember]
        public string CardExpDate { get; set; }
        [DataMember]
        public string AccountNumber { get; set; }
        [DataMember]
        public string NameOnCard { get; set; }
        [DataMember]
        public string CardVV2 { get; set; }
        [DataMember]
        public decimal PaidAmount { get; set; }
        [DataMember]
        public decimal ServiceFee { get; set; }
    }

When I do a Http Post Action, only parts of the Parameters passed to the service.

My request header and body are like below:

User-Agent: Fiddler
Host: localhost:8832
Content-Length: 303
Content-Type: application/xml;charset=utf-8

<Request xmlns="http://Test.com/WCF"&gt;
<AccountNumber>6136307626</AccountNumber>
<NameOnCard>Test test</NameOnCard>
<CardNumber>4000202020202020</CardNumber>
<CardExpDate>11/11</CardExpDate>
<CardVV2>123</CardVV2>
<PaidAmount>30</PaidAmount>
<ServiceFee>1</ServiceFee>
</Request>

The values of CardNumber, CardExpDate and CardVV2 are always not paased to the service. Other fields are good.

Any help is highly appreciated. Thanks!

+3  A: 

DataContract serialization defaults to assuming that members are serialized in alphabetical order instead of the order of declaration unless you specify values for the Order property of the [DataMember] attribute to control ordering explicitly.

Looks like the message you're sending doesn't really match either format, so what's likely happening is that the serializer is assuming some elements are missing and that the out of order ones are extra elements it doesn't know where to put into the CLR class.

So either fix your request so that elements are in alphabetical order, or change the order in the datacontract to match your input message.

tomasr
+1 exactly - Zhao: you should define a clear order on the DataMembers by adding [DataMember(Order=1)] and so forth onto your data.
marc_s
It fix my problem so fast. Thank you, Tomasr! :)
Zhao Wang
Thanks. marc_s. Either way works! You guys are awesome! :)
Zhao Wang