我正在尝试构建一个REST &基于JSON的WCF服务,它将复杂类型作为输入。在客户端上,我试图使用HttpClient作为WCF REST Starter Kit的一部分来使用此服务。错误的请求:使用入门套件的WCF REST服务
下面是我的服务代码:
[WebInvoke(Method = "POST", UriTemplate = "/SendData", BodyStyle = WebMessageBodyStyle.Wrapped)]
public void SendData(List<EditorData> input)
{
//Do something
}
我已经使用,可以在WebMessageBodyStyle枚举无济于事可以找到其他选项。
这里是我的复杂数据类型的合同,我用我的客户,以及:
public class EditorData
{
public string key { get; set; }
public long quesno { get; set; }
public string quescontent { get; set; }
}
客户端代码:
List<EditorData> listEditor = new List<EditorData> { new EditorData { key = "key1", quescontent = "qcontent1", quesno = 1},new EditorData { key = "key2", quescontent = "qcontent2", quesno = 2}};
string jsonEditorList = listEditor.ToJSON();
HttpClient client = new HttpClient("http://localhost/RestWcfService/RestService.svc/");
client.DefaultHeaders.Accept.Add("application/json");
HttpResponseMessage response = null;
response = client.Post("SendData", HttpContent.Create(jsonEditorList));
response.EnsureStatusIsSuccessful();
我的自定义对象列表转换成JSON字符串,我使用我发现的扩展方法here
当我运行此应用程序时,出现以下错误:
BadRequest (400) is not one of the following: OK (200), Created (201), Accepted (202), NonAuthoritativeInformation (203), NoContent (204), ResetContent (205), PartialContent (206)
有什么想法?
编辑:
这里是提琴手截图:
UPDATE:
至于建议由Jason弗雷塔斯,我检查了小提琴手的响应。这是什么东西说:
The server encountered an error processing the request. See server logs for more details.
于是我走进IIS日志,这里是记录在IIS中的错误:
2012-02-15 13:20:08 fe80::ecdd:d2dd:7f70:bef6%11 POST /RestWcfService/RestService.svc/SendData - 80 - fe80::ecdd:d2dd:7f70:bef6%11 - 400 0 0 0
更新2
按Rajesh的建议,我已启用的跟踪为我的wcf服务。下面是由服务器抛出的异常:
The incoming message has an unexpected message format 'Raw'. The expected message formats for the operation are 'Xml', 'Json'. This can be because a WebContentTypeMapper has not been configured on the binding. See the documentation of WebContentTypeMapper for more details.
我还是不明白它是如何获得的时候我已经指定的内容类型为json的Raw格式。
什么在提琴手响应窗口说什么?尝试关闭customErrors以查看是否收到更详细的错误消息。 – 2012-02-15 12:02:34
我用提琴手响应更新了原始帖子。谢谢! – Vinod 2012-02-15 13:24:51
请在我的回答中看到我的更新如下 – Rajesh 2012-02-15 13:59:18