WCF方法没有出错但没有收到我的JSON [英] WCF method not erroring but not receiving my JSON

查看:108
本文介绍了WCF方法没有出错但没有收到我的JSON的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经为WCF Web服务设置了以下接口和类,但是在向其中发布JSON时出现问题,可以很好地发布XML。

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.

[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;
    }
}

如果我发布此XML:

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

我收到此答复(与预期相同):

I get this this response (as expected):

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

但是如果我发布此JSON:

But if I post this JSON:

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

我得到以下答复:

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

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

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

我尝试发布几种不同版本的JSON ,但全部为零。奇怪的是,如果我从发送的JSON中删除了 x和 y属性(例如 { Test:{}} ),实际上并不会出错,但显然参数仍然为零,不确定是否与此相关:)

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. {"Test":{}}), it doesn't actually error, but obviously the parameters are still zero, not sure if this related though :)

推荐答案

感谢Amit Kumar Ghosh我得出了答案是,如果其他人遇到此问题,则我的JSON无法正常工作的原因是因为我正在发布:

Thanks to Amit Kumar Ghosh I worked out the answer, if anyone else has this problem, the reason my JSON wasn't working was because I was posting:

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

但实际上我应该已经发布了:

But in fact I should have been posting this:

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

多谢来自这个问题。

这篇关于WCF方法没有出错但没有收到我的JSON的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆