我有一个简单的对象:
public class Country
{
public int Id { get; set; }
public string Name { get; set; }
}
使用以下操作合同从WCF服务返回:
[OperationContract]
[WebInvoke(Method = "GET", BodyStyle = WebMessageBodyStyle.Wrapped, UriTemplate = "/Countries/{aId}")]
[return: MessageParameter(Name = "Country")]
Country Country(string aId);
基本上,对象包含在Country:{}块中,这就是我想要的。在客户端,我执行以下操作:
private void RequestPrepare(out RestClient aRestClient, out RestRequest aRestRequest, string aRequestUri, Method aRequestMethod = Method.GET)
{
aRestClient = new RestClient(BASE_URL);
aRestRequest = new RestRequest(aRequestUri, aRequestMethod);
aRestRequest.AddHeader("Accept", "application/json");
}
并且这样打电话:
RestClient restClient;
RestRequest restRequest;
RequestPrepare(out restClient, out restRequest, "Countries/{aId}");
restRequest.AddUrlSegment("aId", "1"); // replaces matching token in request.Resource
var restResponse = restClient.Execute<Country>(restRequest);
List<Country> listCountry = new List<Country>();
listCountry.Add(new Country {Id = restResponse.Data.Id, Name = restResponse.Data.Name});
return listCountry;
JSON响应是:
Content = "{\"Country\":{\"Id\":1,\"Name\":\"Australia\"}}"
所以基本上restResponse.Data.Id和restResponse.Data.Name由于“Country”包装而不包含有效数据。
1)那么如何配置RestSharp来处理Country {}换行? 2)作为第二个问题,我如何进行Restsharp调用,以便它自动反序列化List