我有一个简单的对象:
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 不包含有效数据,因为“国家”包装。
1) 那么如何配置 RestSharp 来处理 Country{} 换行?2)作为第二个问题,我如何进行 Restsharp 调用,以便它自动反序列化 List