WCF 方法没有出错但没有收到我的 JSON

WCF method not erroring but not receiving my JSON

我已经为我的 WCF Web 服务设置了以下界面和 class,但是当我 post JSON 到它时遇到了问题,posting XML 工作正常。

[ServiceContract]
public interface IWebService {
    [OperationContract]
    [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;
    }
}

如果我post这个XML:

<Test xmlns="http://tempuri.org/"><x>10</x><y>10</y></Test>

我收到了这个回复(如预期):

<TestResponse xmlns="http://tempuri.org/"><data>20</data></TestResponse> 

但是如果我post这个JSON:

{"Test":{"x":10,"y":10}} 

我收到这样的回复:

<TestResponse xmlns="http://tempuri.org/"><data>0</data></TestResponse> 

当我在方法上放置断点时,我看到 x 和 y 参数均为 0。

我已经尝试 posting 我的 JSON 的几个不同版本,但都以零出现。奇怪的是,如果我从我发送的 JSON 中删除 'x' 和 'y' 属性(例如 {"Test":{}}),它实际上并没有错误,但显然参数仍然为零, 不确定这是否相关:)

对于这个请求示例 -

{"Test":{"x":10,"y":10}} 

服务联系人应该类似于 -

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

其中 -

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

}

感谢 Amit Kumar Ghosh 我找到了答案,如果其他人有这个问题,我的 JSON 没有工作的原因是因为我发帖:

{"Test":{"x":10,"y":10}} 

但实际上我应该发布这个:

{"x":10,"y":10}

感谢 Amit Kumar Ghosh 和 Konrad Kokosa 来自 this 个问题。