我正在嘗試構建一個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