【发布时间】:2017-03-23 04:54:20
【问题描述】:
我已经为我的 WCF Web 服务设置了以下接口和类,但是当我向它发布 JSON 时遇到问题,发布 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;
}
}
如果我发布此 XML:
<Test xmlns="http://tempuri.org/"><x>10</x><y>10</y></Test>
我收到了这个回复(如预期的那样):
<TestResponse xmlns="http://tempuri.org/"><data>20</data></TestResponse>
但如果我发布这个 JSON:
{"Test":{"x":10,"y":10}}
我收到以下回复:
<TestResponse xmlns="http://tempuri.org/"><data>0</data></TestResponse>
当我在方法上设置断点时,我看到 x 和 y 参数都是 0。
我尝试过发布我的 JSON 的几个不同版本,但都以零的形式出现。奇怪的是,如果我从我发送的 JSON 中删除 'x' 和 'y' 属性(例如{"Test":{}}),它实际上并没有错误,但显然参数仍然为零,但不确定这是否相关:)
【问题讨论】:
-
您正在使用
WebMessageBodyStyle.Wrapped,但您的服务合同似乎不接受任何服务。
标签: c# asp.net json wcf .net-4.6.1