如何将List <t>发布到WCF Rest服务</t>

时间:2011-06-16 14:08:08

标签: wcf rest post service

我有一个班级

[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 个答案:

答案 0 :(得分:1)

List等效于数组,因此该值应表示为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 of Bare或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"}
]