HttpContent.ReadAsAsync反序列化问题 [英] HttpContent.ReadAsAsync Deserialization issue

查看:1286
本文介绍了HttpContent.ReadAsAsync反序列化问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有我通过HttpSelfHostServer(ASP.Net的WebAPI测试版)运行的小型REST服务,但我有反序列化被发送到服务器的一些数据的一些问题。该方法的签名如下:

I have a small REST service that I am running via the HttpSelfHostServer (ASP.Net WebApi Beta), but have am having some problems deserializing some data that is posted to the server. The method signature is as follows:

public HttpResponseMessage PostServers(ServerType serverType)

该方法被称为正常,但是使用以下code中的数据的反序列化:

The method is being called fine, however on deserialization of the data using the following code:

var servers = Request.Content.ReadAsAsync<List<ServerZoneInformation>>().Result;
...

则抛出IOException消息无法访问已关闭的流。试图通过反序列化

an IOException is thrown with the message "Cannot access a closed Stream.". The same error occurs when trying to deserialize the code via

XmlSerializer serializer = new XmlSerializer(typeof(List<ServerZoneInformation>));
var servers = (List<ServerZoneInformation>)serializer.Deserialize(Request.Content.ReadAsStreamAsync().Result);
... 

不过,我可以得到该方法,如果我使用以下工作:

However I can get the method to work if I use the following:

XmlSerializer serializer = new XmlSerializer(typeof(List<ServerZoneInformation>));
string data = Request.Content.ReadAsStringAsync().Result;
using (MemoryStream ms = new MemoryStream(UTF8Encoding.UTF8.GetBytes(data)))
{
   var servers = (List<ServerZoneInformation>)serializer.Deserialize(ms);
   ...
}

我在做什么错的财产以后的前两种情况,或者这是在的WebAPI的错误吗?

Am I doing somthing wrong in the first two cases, or is this a bug in the WebApi?

请注意:通过IIS托管然而,当我还没有试过这种

Note: I have not tried this when hosting via IIS yet.

干杯

推荐答案

在的WebAPI的目前的测试版跟一个MS人ASP.Net网站上的错误之后,更多信息的此处。这个问题应固定在的WebAPI的下一个版本。

After talking to a MS person on the ASP.Net website its a bug in the current beta of the WebApi, more info here. This problem should be fixed in the next release of the WebApi.

这篇关于HttpContent.ReadAsAsync反序列化问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆