How to create JSON to match/serialize to DataContract in WCF Rest Service

后端 未结 3 820
梦谈多话
梦谈多话 2021-01-20 03:34

Interface:

namespace SQRT_WCF
    {
    [DataContract]
    public class PlaceOrder
    {
        [DataMember]
        public string claimID { get; set; }
            


        
相关标签:
3条回答
  • 2021-01-20 04:11

    Answer was buried here:

    WCF BodyStyle WrappedRequest doesn't work for incoming JSON param?

    The name of the wrapper is not the parameter type, but the parameter name.

    So in my case, the wrapper name was "order", because my parm name is "order":

    SQ_jsonModel.Response placeOrderSQRT(PlaceOrder order);
    

    The following works from SOAP-UI now:

    {
        "order" : {
             "claimID" : "value",
             "rederenceDate" : "value"
    }
    

    To me it's very shocking; normally we can change variable names and parms names (just not Class Names) with absolute no impact on the user interface. The reference post shows how you can use the attribute "MessageParameter" to set the external name, and still allow one to change the internal name if needed.

    0 讨论(0)
  • 2021-01-20 04:12

    I think you might need to stringify your object.

    var obj = {
         "claimID" : "value",
         "rederenceDate" : "value"
     }
    
    .......
    
    data: '{ "argPlaceOrderJSON": ' + JSON.stringify(obj) + ' }'
    
    0 讨论(0)
  • 2021-01-20 04:23

    I had a similar issue and turned out that I had to define my request format and body. Try this:

    [OperationContract]
    [WebInvoke(Method = "POST", BodyStyle = WebMessageBodyStyle.WrappedRequest, RequestFormat = WebMessageFormat.Json)]
    YourResultType placeOrder(PlaceOrder argPlaceOrderJSON);
    
    0 讨论(0)
提交回复
热议问题