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