2011-06-16 41 views
0

我有一個類如何列表<T>張貼到WCF REST服務

[DataContract] 
public class Test 
{ 
[DataMemeber] 
public string A {get;set;} 
[DataMemeber] 
public string B {get;set;} 
[DataMemeber] 
public string C {get;set;} 
} 

我有一個RESTful的WCF方法

[WebInvoke(UriTemplate = "checkupdates", 
ResponseFormat = WebMessageFormat.Json, 
BodyStyle=WebMessageBodyStyle.WrappedRequest)] 

List<Test> CheckForUpdates(List<Test> testing); 

我怎樣才能發佈List對象的服務?這是來自wpf客戶端。

由於

回答

1

列表相當於一個數組,所以該值應被表示爲一個JSON陣列。而且由於車身造型說,請求需要包裝,那麼你應該用一個命名爲參數字段包裹JSON數組中的對象:

{"testing":[ 
    {"A":"Value of A1","B":"Value of B1","C":"Value of C1"}, 
    {"A":"Value of A2","B":"Value of B2","C":"Value of C2"}, 
    {"A":"Value of A3","B":"Value of B3","C":"Value of C3"}]} 

如果該請求沒有被包(裸BodyStyle或WrappedResponse),那麼你將不需要包裝對象,這將是一個請求的操作:

[ 
    {"A":"Value of A1","B":"Value of B1","C":"Value of C1"}, 
    {"A":"Value of A2","B":"Value of B2","C":"Value of C2"}, 
    {"A":"Value of A3","B":"Value of B3","C":"Value of C3"} 
] 
+0

是否有可能給你一個例子。將改變BodyStyle使生活更容易? – Bullish 2011-06-16 14:26:58

+0

如果BodyStyle是Bare(或WrappedResponse,意味着請求未被包裝),那麼你不需要包裝對象 - 我會更新與Bare案例的請求的答案。 – carlosfigueira 2011-06-16 16:00:36