2014-07-07 17 views
0

我有一個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標識符。

回答

0

設置BodyStyle = WebMessageBodyStyle.Bare修正了該問題。

0

您需要訪問response.Data而不是response.Content以獲取反序列化的對象。

+0

問題是BodyStyle = WebMessageBodyStyle.Wrapped。將其設置爲「裸」可解決問題。 – 1232133d2ffa