0

I have WCF method

[OperationContract]
[WebInvoke(Method = "POST",
RequestFormat = WebMessageFormat.Json,
ResponseFormat = WebMessageFormat.Json,
BodyStyle = WebMessageBodyStyle.Wrapped)]
string PostNewOrder(string OrderData);

This is the json string which I am posting

{
    "customerId": " ",
    "langCode": "SE",
    "timeZone": "38",
    "orderNumber": "1122519",
    "orderDate": "2016-04-13 15:56:36",
    "deliveryNumber": "625615",
    "devices": "000000001050840;",
    "transactionId": "24",
    "shipDate": "2016-04-13 16:41:31"
}

But I am getting OrderData as null in the WCF method

If I post string

"{\"customerId\":\" \",\"langCode\":\"SE\",\"timeZone\":\"38\",\"orderNumber\":\"1122519\",\"orderDate\":\"2016-04-13 15:56:36\",\"deliveryNumber\":\"625615\",\"devices\":\"000000001050840;\",\"transactionId\":\"24\",\"shipDate\":\"2016-04-13 16:41:31\"}"

It works fine but this is not a proper json , Thanks in Advance..

2
  • 2
    Can you show us the code where you convert your object to the json string? Commented Apr 14, 2016 at 10:31
  • No i am just using post man client to post the json string to wcf method. Commented Apr 14, 2016 at 10:49

3 Answers 3

1

your contract should look something like -

    [OperationContract]
    [WebInvoke(Method = "POST",
    RequestFormat = WebMessageFormat.Json,
    ResponseFormat = WebMessageFormat.Json ,
    UriTemplate="/post")]
    string PostNewOrder(RootObject OrderData);

where RootObject should look like -

public class RootObject
{
    public string customerId { get; set; }
    public string langCode { get; set; }
    public string timeZone { get; set; }
    public string orderNumber { get; set; }
    public string orderDate { get; set; }
    public string deliveryNumber { get; set; }
    public string devices { get; set; }
    public string transactionId { get; set; }
    public string shipDate { get; set; }
}

what you are posting is a json object representation and not a string and the WCF Runtime is expected to deserialize the content to it's equivalent strongly typed object at server.

Sign up to request clarification or add additional context in comments.

Comments

0

I think you need to remove RequestFormat and ResponseFormat from OperationContract

3 Comments

ya i tried this but i am getting this error :The server encountered an error processing the request. The exception message is 'There was an error deserializing the object of type System.String. End element 'root' from namespace '' expected. Found element 'customerId' from namespace ''.'.
Like That: var text = '{ "employees" : [' + '{ "firstName":"John" , "lastName":"Doe" },' + '{ "firstName":"Anna" , "lastName":"Smith" },' + '{ "firstName":"Peter" , "lastName":"Jones" } ]}';
the request payload is not even a string
0

this is an old question, but in case anyone needs an answer for this:

you will need to wrap your json string in anoter string.

i was using angular - so in my case i did this:

let doc = new FileData();
...
let jsonObj = JSON.stringify(doc); //json object string

let strObj = JSON.stringify(jsonObj); //json object string wrapped with string

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.