2012-02-15 55 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

相關問題