I have a program that calls a rest service and gets back a string array. When I try to read the string array I get an exception Unexpected end of file. If I have the service return a string containing just the first item of the array it works. What am I missing about ReadAsDataContract<>() that is causing this exception?
var returnElement = response.Content.ReadAsDataContract<string[]>();
The exception is an XMLException: Unexpected End of File.
Stack trace-
at System.Xml.EncodingStreamWrapper.ReadBOMEncoding(Boolean notOutOfBand)
at System.Xml.EncodingStreamWrapper..ctor(Stream stream, Encoding encoding)
at System.Xml.XmlUTF8TextReader.SetInput(Stream stream, Encoding encoding, XmlDictionaryReaderQuotas quotas, OnXmlDictionaryReaderClose onClose)
at System.Runtime.Serialization.XmlObjectSerializer.ReadObject(Stream stream)
at System.Runtime.Serialization.DataContractContentExtensions.ReadAsDataContract[T](HttpContent content, DataContractSerializer serializer)
at System.Runtime.Serialization.DataContractContentExtensions.ReadAsDataContract[T](HttpContent content)
at RestConsumption.Program.GetDevices() in **********\documents\visual studio 2010\Projects\RestPractice\RestConsumption\Program.cs:line 55
at RestConsumption.Program.Main(String[] args) in *********\documents\visual studio 2010\Projects\RestPractice\RestConsumption\Program.cs:line 26
at System.AppDomain._nExecuteAssembly(RuntimeAssembly assembly, String[] args)
at System.AppDomain.ExecuteAssembly(String assemblyFile, Evidence assemblySecurity, String[] args)
at Microsoft.VisualStudio.HostingProcess.HostProc.RunUsersAssembly()
at System.Threading.ThreadHelper.ThreadStart_Context(Object state)
at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean ignoreSyncCtx)
at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state)
at System.Threading.ThreadHelper.ThreadStart()
I solved this in a really easy way that I still don’t understand why it worked. I simply did
instead of
and it worked fine. If anyone tells me why this worked I’ll give them credit for answering this question.