我有一個RESTful WCF服務函數,但它不通過RestSharp客戶端正確序列化。RestSharp JSON不非傳遞
[ServiceContract]
public interface IRestDragon
{
[OperationContract(Name = "getconfig")]
[WebInvoke(Method = "GET", UriTemplate = "getconfig/{id}", BodyStyle = WebMessageBodyStyle.Wrapped,
ResponseFormat = WebMessageFormat.Json, RequestFormat = WebMessageFormat.Json)]
DragonConfig GetConfig(string id);
}
public class RestDragon : IRestDragon
{
public DragonConfig GetConfig(string id)
{
var config = new DragonConfig {Name = "Test", Data = "DAtaaaaaa"};
return config;
}
}
這裏是我如何消費服務:
static void Main(string[] args)
{
Console.ReadLine();
var client = new RestClient("http://localhost:5463/RESTDragon.svc");
client.AddDefaultHeader("ContentType", "application/json");
var request = new RestRequest("/getconfig/11123") {Method = Method.GET, RequestFormat = DataFormat.Json};
var response = client.Execute<DragonConfig>(request);
Console.WriteLine("Response: " + response.Content);
Console.ReadLine();
}
但是它返回:
Response: {"getconfigResult":{"Data":"DAtaaaaaa","Name":"Test"}}
我無法通過response.Data訪問反序列化數據。 *。它返回爲空,並且數據顯示在Content中,但是以JSON格式顯示,並帶有奇怪的getconfigResult標識符。
問題是BodyStyle = WebMessageBodyStyle.Wrapped。將其設置爲「裸」可解決問題。 – 1232133d2ffa