简体   繁体   English

ASP.NET Web API ReadAsAsync失败,没有错误

[英]ASP.NET Web API ReadAsAsync fails with no error

I'm on my first forray into ASP.NET Web API and I've got a problem which is causing me a great deal of frustration. 我第一次涉足ASP.NET Web API,但遇到了一个问题,这使我感到非常沮丧。

The RESTful service that I'm calling returns data such as: 我正在调用的RESTful服务返回的数据如下:

<SearchResults  xmlns:i="http://www.w3.org/2001/XMLSchema-instance">
    <Results>
        <Result>
            <Title>foo</Title>
        </Result>
        <Result>
            <Title>bar</Title>
        </Result>
    </Results>
    <NumberOfResults>2</NumberOfResults>
</SearchResults>

So I've created some entities to represent this data: 因此,我创建了一些实体来表示此数据:

[Serializable, DataContract(Namespace = "")]
public class SearchResults
{
    public List<Result> Results { get; set; }
    public int NumberOfResults { get; set; }
}

[Serializable, DataContract(Namespace = "")]
public class Result
{
    public string Title { get; set; }
}

And then I attempted to deserialize the data: 然后,我尝试反序列化数据:

var client = new HttpClient();
client.BaseAddress = new Uri("some uri");

var response = client.GetAsync("/some/path/").Result;
response.EnsureSuccessStatusCode();

var result = response.Content.ReadAsAsync<SearchResults>().Result;

Which results in a SearchResults object with Results = null and NumberOfResults = 0 even though I know that there are results being returned, and if I check response.Content.ReadAsStringAsync(), the XML contains the results as expected. 即使我知道有返回的结果,并且如果我检查response.Content.ReadAsStringAsync(),XML也会包含预期的结果,结果是结果为null且NumberOfResults = 0的SearchResults对象。

I instead tried: 我改为尝试:

var serializer = new XmlSerializer(typeof(SearchResults));
var results = (SearchResults)serializer.Deserialize(response.Content.ReadAsStreamAsync().Result);

And that returned a fully populated SearchResults object. 然后返回一个完全填充的SearchResults对象。

Finally, I tried implementing a simple IFormatterLogger and passing that into ReadAsAsync, which got called if I tried to read the stream twice (as expected!), but doesn't get called during a standard attempt at deserialization using ReadAsAsync. 最后,我尝试实现一个简单的IFormatterLogger并将其传递给ReadAsAsync,如果我尝试两次读取流(如预期的那样),则会调用该方法,但在使用ReadAsAsync进行反序列化的标准尝试期间不会被调用。

I could just use an XmlSerializer and pass it the steam since that works, but that doesn't seem as neat and tidy as using ReadAsAsync, plus I really want to know what I'm doing wrong :) 我可以使用XmlSerializer并向其传递蒸汽,因为它可以正常工作,但这似乎不像使用ReadAsAsync那样整洁,再加上我真的很想知道我在做错什么:)

Updated 16:38BST 03/09/2012 更新的16:38BST 03/09/2012

Ok, I'm clearly missing something of vital importance when using Web API, but I still dont know what! 好的,在使用Web API时,我显然缺少了一些至关重要的东西,但是我仍然不知道是什么!

I've now tried consuming a service with a PUT verb, and whilst my entity serializes properly when I tested it using XmlSerializer the request was failing with an HTTP 500. I checked what was being sent with Fiddler and the XML generated by Web API looks nothing like that generated by XmlSerializer. 现在,我尝试使用带有PUT动词的服务,并且当我使用XmlSerializer测试实体时,我的实体可以正确地序列化时,该请求使用HTTP 500失败。我检查了Fiddler发送的内容以及Web API生成的XML外观没有像XmlSerializer生成的那样。 For a start, everthing has names like "_x003C_Name_x003E_k__BackingField" rather than simply "Name". 首先,所有内容都具有“ _x003C_Name_x003E_k__BackingField”之类的名称,而不是简单的“ Name”。

The weird "k__BackingField" names from my update have led me to the resolution, and as usual it was something very simple. 我的更新中出现了怪异的“ k__BackingField”名称,导致我找到了解决方法,并且像往常一样,这非常简单。

I'd forgotten to mark the properties in the entities with the DataMemberAttribute. 我忘了用DataMemberAttribute标记实体中的属性。

Marked them up and it all started working as I originally expected it to! 标记它们,一切按我最初的预期开始工作!

That was all it was! 仅此而已!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM