Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
310 views
in Technique[技术] by (71.8m points)

datacontractserializer - WCF Datacontract, some fields do not deserialize

Problem:

I have a WCF service setup to be an endpoint for a call from an external system. The call is sending plain xml. I am testing the system by sending calls into the service from Fiddler using the RequestBuilder.

The issue is that all of my fields are being deserialized with the exception of two fields. price_retail and price_wholesale.

What am I missing? All of the other fields deserialize without an issue - the service responds. It is just these fields.

XML Message:

<widget_conclusion>
    <list_criteria_id>123</list_criteria_id>
    <list_type>consumer</list_type>
    <qty>500</qty>
    <price_retail>50.00</price_retail>
    <price_wholesale>40.00</price_wholesale>
    <session_id>123456789</session_id>
</widget_conclusion>

Service Method:

public string WidgetConclusion(ConclusionMessage message)
{
    var priceRetail = message.PriceRetail;
}

Message class:

[DataContract(Name = "widget_conclusion", Namespace = "")]
public class ConclusionMessage  
{
    [DataMember(Name = "list_criteria_id")]
    public int CriteriaId  { get; set;}
    [DataMember(Name = "list_type")]
    public string ListType { get; set; }
    [DataMember(Name = "qty")]
    public int ListQuantity { get; set; }
    [DataMember(Name = "price_retail")]
    public decimal PriceRetail { get; set; }
    [DataMember(Name = "price_wholesale")]
    public decimal PriceWholesale { get; set; }
    [DataMember(Name = "session_id")]
    public string SessionId { get; set; }
}
Question&Answers:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Fields are in the wrong order for your message. DataContracts default to Alphabetical ordering and not order of declaration; and expects XML elements to arrive in that order; Out of order elements are discarded usually.

Either fix your contract to specify the right order explicitly (using the Order property of the DataMemberAttribute) or make sure your client sends them in the right one.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...