我使用RestSharp。
我有一个列表private List<string> _GuyIds;
然后
[TestMethod]
public void GuyMetadataServiceShouldReturnExpected()
{
var client = new RestClient("http://localhost/OrionServices/GuyService.svc/");
var request = new RestRequest("{facilityId}/Guys/metadata/", Method.POST);
request.AddUrlSegment("facilityId", "888");
request.AddParameter("GuyIds", JsonConvert.SerializeObject(_GuyIds.ToArray()), ParameterType.RequestBody);
var response = client.Execute(request);
dynamic data = JsonConvert.DeserializeObject<dynamic>(response.Content);
在服务中,
[OperationContract]
[WebInvoke(Method = "POST", BodyStyle = WebMessageBodyStyle.WrappedResponse, RequestFormat = WebMessageFormat.Json, ResponseFormat = WebMessageFormat.Json, UriTemplate = "/{FacilityID}/Guys/metadata/")]
public List<dynamic> GenerateMetadata(string FacilityID)
{
WebOperationContext.Current.OutgoingResponse.Headers.Add("Cache-Control", "no-cache, must-revalidate");
WebOperationContext.Current.OutgoingResponse.Headers.Add("Expires", "Sat, 26 Jul 1997 05:00:00 GMT");
var Guys = Encoding.UTF8.GetString(OperationContext.Current.RequestContext
.RequestMessage.GetBody<byte[]>());
当我将鼠标悬停在服务代码中的Guys
上时,我得到了
["0001","0003","0004","0005","0006"]
但是,当我检查响应时,内容为空。当我将数组传递给此行中的请求时,我猜错了。
request.AddParameter("GuyIds", JsonConvert.SerializeObject(_GuyIds.ToArray()), ParameterType.RequestBody);
如何解决?
答案 0 :(得分:0)
自己弄清楚。 WCF不支持序列化动态类型。我必须定义一个POCO对象。
[OperationContract]
[WebInvoke(Method = "POST", BodyStyle = WebMessageBodyStyle.WrappedResponse, RequestFormat = WebMessageFormat.Json, ResponseFormat = WebMessageFormat.Json, UriTemplate = "/{FacilityID}/Guys/metadata/")]
public List<GuysDTO> GenerateMetadata(string FacilityID)
{
WebOperationContext.Current.OutgoingResponse.Headers.Add("Cache-Control", "no-cache, must-revalidate");
WebOperationContext.Current.OutgoingResponse.Headers.Add("Expires", "Sat, 26 Jul 1997 05:00:00 GMT");
然后
public class GuyDTO
{
public string metadata1 {get;set;}
....
}