2012-02-15 77 views
2

我正在尝试构建一个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) 

有什么想法?

编辑:

这里是提琴手截图:

enter image description here

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格式。

+1

什么在提琴手响应窗口说什么?尝试关闭customErrors以查看是否收到更详细的错误消息。 – 2012-02-15 12:02:34

+0

我用提琴手响应更新了原始帖子。谢谢! – Vinod 2012-02-15 13:24:51

+0

请在我的回答中看到我的更新如下 – Rajesh 2012-02-15 13:59:18

回答

4

首先尝试在你的WCF服务使Tracing看到400错误的请求错误的确切原因。

似乎所发布的输入格式不正确。您已经将EditorData列表定义为该方法的参数并发布了一些键值对(请参阅您的fiddler屏幕快照)确保反序列化时fiddler中的json字符串转换为EditorData对象列表。

此外,您还设置了包裹的身体风格。尝试删除它,看看它是否工作。当你有多个参数时,会使用包装请求,然后在这种情况下,将所有参数包装在方法名称元素中并通过线路发送。

UPDATE:

请务必在Content-Type添加到头部,如下图所示:

client.DefaultHeaders.ContentType = "application/json"; 
+0

感谢您的意见Rajesh,我启用了跟踪服务,并且使用我的发现更新了原始文章(UPDATE2)。 – Vinod 2012-02-15 13:46:00

+1

请参阅我的更新,并且一旦添加了内容类型那应该可以解决你的错误 – Rajesh 2012-02-15 13:58:30

+1

超级!就是这样!!!谢谢百万:-) – Vinod 2012-02-15 14:08:02

0

[WebInvoke]默认为XML序列化。您需要告诉它,您正在将数据作为JSON发送。坐落在WebInvoke的RequestFormat属性这样

RequestFormat = WebMessageFormat.Json

+0

试过这个,还是一样的错误! :-( – Vinod 2012-02-15 09:30:53

+0

这不起作用,似乎没有任何影响的服务 - 非常不幸的,因为我认为它会做同样的事情 - 更好的问题是为什么这个heck wcf无法弄清楚json vs XML的请求体 - 看起来似乎非常简单,在WebApi中的工作方式如下: - – schmoopy 2013-06-13 00:58:55

相关问题