MadSkunk MadSkunk - 1 year ago 67
JSON Question

WCF method not erroring but not receiving my JSON

I have setup the following interface and class for my WCF web service, but am having a problem when I post JSON to it, posting XML works fine.

public interface IWebService {
[WebInvoke(Method = "POST", ResponseFormat = WebMessageFormat.Xml, BodyStyle = WebMessageBodyStyle.Wrapped, UriTemplate = "xml/post")]
[return: MessageParameter(Name = "data")]
int Test(int x, int y);

public class WebService : IWebService {
public int Test(int x, int y) {
return x + y;

If I post this XML:

<Test xmlns=""><x>10</x><y>10</y></Test>

I get this this response (as expected):

<TestResponse xmlns=""><data>20</data></TestResponse>

But if I post this JSON:


I get this response:

<TestResponse xmlns=""><data>0</data></TestResponse>

And when I put a breakpoint on the method I see that the x and y parameters are both 0.


I've tried posting several different versions of my JSON, but all come through as zeros. Oddly, if I remove the 'x' and 'y' properties from the JSON I send (e.g.
), it doesn't actually error, but obviously the parameters are still zero, not sure if this related though :)

Answer Source

For this sample of request -


The Service contact should look something like -

public int Test(Model Test) {
    return test.x + test.y;

where -

public class Model{
    public int x { get; set; }
    public int y { get; set; }

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download