【问题标题】:RestSharp example not workingRestSharp示例不起作用
【发布时间】:2012-04-24 13:17:38
【问题描述】:

下面的代码是从http://www.stum.de/2009/12/22/using-restsharp-to-consume-restful-web-services/ 更新的。我已经更新了最新版本的 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 数据(参见下面的截图)。有人可以帮我看看我是否遗漏了什么吗?

截图:

错误消息显示

“参数计数不匹配。”

返回的 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)

【问题讨论】:

  • 我怀疑 RestSharp XMLDeserializer 可能无法协商 XML 如何映射到模型。我注意到模型没有附加 Serializable 或 DataContract 属性。您的服务端点是如何形成要返回的 XML 的?能详细介绍一下吗?
  • @Jesse Squre,请查看我返回的 XML 更新以及错误消息。
  • 是否可以将堆栈跟踪与消息一起发布?消息本身并没有告诉我们太多。
  • 绝对@JesseSquire,我已经用堆栈和跟踪更新了这个问题。

标签: c# restsharp


【解决方案1】:

在查看堆栈跟踪时,肯定与反序列化 XML 的能力有关。您遵循的教程是 2009 年的,看起来 RestSharp 已经从它下面发生了一些变化。

我能够让您的样本正常工作,并使用“plants.Data”访问植物列表,只需稍作改动:

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

变成

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

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

希望对您有所帮助。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2015-10-28
    • 1970-01-01
    • 1970-01-01
    • 2012-07-05
    • 2013-12-04
    • 2016-08-31
    • 2016-02-25
    相关资源
    最近更新 更多