2

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)

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

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

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);
   ...
}

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

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

Cheers

Ben

j0k
  • 22,600
  • 28
  • 79
  • 90
BBoy
  • 1,063
  • 1
  • 10
  • 21

1 Answers1

2

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.

BBoy
  • 1,063
  • 1
  • 10
  • 21