2012-04-24 138 views
0

我已经从http://www.stum.de/2009/12/22/using-restsharp-to-consume-restful-web-services/更新了以下代码。我已经更新为最新版本RestSharp的:RestSharp示例不起作用

var client = new RestClient("http://carma.org"); 
var request = new RestRequest("api/1.1/searchPlants", Method.GET); 
request.AddParameter("location", 4338); 
request.AddParameter("limit", 10); 
request.AddParameter("color", "red"); 
request.AddParameter("format", "xml"); 
var plants = client.Execute<PowerPlantsDTO>(request); 
MessageBox.Show(plants.Count.ToString()) 


using System.Collections.Generic; 

namespace RestTest.Model 
{ 
    public class CityDTO 
    { 
     public string value { get; set; } 
    } 

    public class LocationDTO 
    { 
     public CityDTO city { get; set; } 
     public int zip { get; set; } 
     public double latitude { get; set; } 
     public double longitude { get; set; } 
    } 

    public class item 
    { 
     public string name { get; set; } 
     public LocationDTO location { get; set; } 
    } 

    public class PowerPlantsDTO : List<item> { } 
} 

不幸的是,plants.count是空的,所以作为plants.data但有一个XML数据返回(见下文screeshots)。是否有人可以帮助我,我是否失去了什么?

截图: enter image description here

enter image description here

的Errmessage显示

“参数计数不匹配。”返回

和XML内容:

<?xml version="1.0" encoding="UTF-8"?> 
<items> 
<item> 
    <id>49046</id> 
    <name>WATSON COGEN</name> 
    <carbon> 
     <past>4503176.0000</past> 
     <present>4582168.0000</present> 
     <future>5401482.0000</future> 
    </carbon> 
    <energy> 
     <past>3827727.0000</past> 
     <present>3017826.0000</present> 
     <future>3506896.0000</future> 
    </energy> 
    <intensity> 
     <past>2352.9250</past> 
     <present>3036.7339</present> 
     <future>3080.4910</future> 
    </intensity> 
    <location> 
     <continent> 
      <id>5</id> 
      <value>North America</value> 
     </continent> 
     <country> 
      <id>202</id> 
      <value>United States</value> 
     </country> 
     <latitude>33.8219</latitude> 
     <longitude>-118.2633</longitude> 
     <state> 
      <id>644</id> 
      <value>California</value> 
     </state> 
     <city> 
      <id>60769</id> 
      <value>Carson</value> 
     </city> 
     <metroarea> 
      <id>3203</id> 
      <value>Los Angeles-Long Beach</value> 
     </metroarea> 
     <county> 
      <id>4338</id> 
      <value>Los Angeles</value> 
     </county> 
     <congdist> 
      <id>5298</id> 
      <value>Diane Watson</value> 
     </congdist> 
     <zip>90749</zip> 
    </location> 
</item> 
<item> 
    <id>7233</id> 
    <name>CARSON COGEN</name> 
    <carbon> 
     <past>432223.9062</past> 
     <present>440564.3125</present> 
     <future>451224.5000</future> 
    </carbon> 
    <energy> 
     <past>461797.6875</past> 
     <present>348148.4062</present> 
     <future>355428.0938</future> 
    </energy> 
    <intensity> 
     <past>1871.9189</past> 
     <present>2530.8989</present> 
     <future>2539.0481</future> 
    </intensity> 
    <location> 
     <continent> 
      <id>5</id> 
      <value>North America</value> 
     </continent> 
     <country> 
      <id>202</id> 
      <value>United States</value> 
     </country> 
     <latitude>33.8759</latitude> 
     <longitude>-118.2491</longitude> 
     <state> 
      <id>644</id> 
      <value>California</value> 
     </state> 
     <city> 
      <id>60769</id> 
      <value>Carson</value> 
     </city> 
     <metroarea> 
      <id>3203</id> 
      <value>Los Angeles-Long Beach</value> 
     </metroarea> 
     <county> 
      <id>4338</id> 
      <value>Los Angeles</value> 
     </county> 
     <congdist> 
      <id>5433</id> 
      <value>Juanita Millender-McDonald</value> 
     </congdist> 
     <zip>90746</zip> 
    </location> 
</item> 

</items> 

堆栈跟踪:

at System.Reflection.RuntimeMethodInfo.Invoke(Object obj, BindingFlags invokeAttr, Binder binder, Object[] parameters, CultureInfo culture, Boolean skipVisibilityChecks) 
    at System.Reflection.RuntimeMethodInfo.Invoke(Object obj, BindingFlags invokeAttr, Binder binder, Object[] parameters, CultureInfo culture) 
    at System.Reflection.RuntimePropertyInfo.SetValue(Object obj, Object value, BindingFlags invokeAttr, Binder binder, Object[] index, CultureInfo culture) 
    at System.Reflection.RuntimePropertyInfo.SetValue(Object obj, Object value, Object[] index) 
    at RestSharp.Deserializers.XmlDeserializer.Map(Object x, XElement root) 
    at RestSharp.Deserializers.XmlDeserializer.HandleListDerivative(Object x, XElement root, String propName, Type type) 
    at RestSharp.Deserializers.XmlDeserializer.Deserialize[T](IRestResponse response) 
    at RestSharp.RestClient.Deserialize[T](IRestRequest request, IRestResponse raw) 
+0

我怀疑RestSharp XMLDeserializer可能无法协商XML如何映射到模型。我注意到这些模型没有附加Serializable或DataContract属性。您的服务端点如何形成正在返回的XML?你能展示它的细节吗? – 2012-04-24 14:19:44

+0

@Jesse Squre,请参阅我的更新XML返回的错误消息。 – 2012-04-24 22:11:16

+0

是否可以将堆栈跟踪与消息一起发布?这个信息本身并没有说明什么。 – 2012-04-25 19:22:39

回答

0

在看堆栈跟踪,它肯定是与反序列化XML的能力。您正在遵循的教程是从2009年开始的,看来RestSharp已经从它下面改变了一些。

我能得到你的样品工作,并访问使用“plants.Data`植物的名单,一个小的变化:

var plants = client.Execute<PowerPlantsDTO>(request); 

成为

var plants = client.Execute<List<item>>(request); 

通过寻找在RestSharp代码中,似乎他们使用XmlDeserializer内的通用参数名称来确定XML节点名称。出于好奇,我快速浏览了使用XmlElement属性尝试影响XML节点的名称,但该属性不能应用于类。据我所知,你最好的选择是塑造你的DTO以明确地匹配预期的回报结构。

希望有所帮助。